They can't even describe simple problems without using escape hatches (ignoring C++'s crazy templates).
Example:
Everyone in your system has a role. There are 3 kind of roles: User, Moderator, Admin
They have different attributes. All of them have a name, but only moderators and admins have dedicates areas of responsibility. And only moderators have a subset of at least one allowed permission (e.g. "edit posts" and/or "delete posts").
You can't really denote this in a way that the Java compiler understands it at the use site. E.g. an action is executed and you want to see if it is allowed. So you want to check the kind of role and if the attributes/permissions match the action or not. The compiler doesn't support this well at ala.
So the problem is that the type system does not give you the type of the caller of a method? Does e.g. Haskell support this?
(As an aside, I think one solution in Java would be to use the command pattern and pass an object (ModeratorCommand, etc.) to whatever code is processing your actions, so it can type check the command.)
It's more that you simply can't define your datastructure correctly.
> Does e.g. Haskell support this?
Sure, and it is not certainly not the only language. E.g.:
data Role = User String | Moderator String [Area] NonEmpty Permission | Admin String [Area]
I'm not even saying that this is the best way of modeling it - but with Java you unfortunately can't really define it.
> (As an aside, I think one solution in Java would be to use the command pattern and pass an object (ModeratorCommand, etc.) to whatever code is processing your actions, so it can type check the command.)
The problem stays the same though: how do you know what kind of command it is that you just received? You cannot safely "match" on the command in the same way you can in other languages, because the compiler doesn't know that there are only exactly 3 types of commands.
Okay, so it's union types plus pattern matching that are missing (among other things I'm sure). Thanks, that's helpful.
> You cannot safely "match" on the command in the same way you can in other languages, because the compiler doesn't know that there are only exactly 3 types of commands.
Perhaps the visitor pattern? Command processing receives an object and calls its command method, which knows what permissions it has.
I guess the fact that I have to keep saying "patterns" is a hint that some flexibility is missing. Still, I'd rather have Java/C++ static types than nothing at all.
Yes the visitor pattern is a valid encoding of algebraic datatypes. Very heavyweight (imagine if booleans were all encoded with the visitor pattern) but nonetheless valid.
Example: Everyone in your system has a role. There are 3 kind of roles: User, Moderator, Admin
They have different attributes. All of them have a name, but only moderators and admins have dedicates areas of responsibility. And only moderators have a subset of at least one allowed permission (e.g. "edit posts" and/or "delete posts").
You can't really denote this in a way that the Java compiler understands it at the use site. E.g. an action is executed and you want to see if it is allowed. So you want to check the kind of role and if the attributes/permissions match the action or not. The compiler doesn't support this well at ala.