Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.

https://blog.ploeh.dk/2018/06/25/visitor-as-a-sum-type/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: