Hacker News new | ask | show | jobs
by grumpyprole 844 days ago
> You're confusing unrelated features

Actually I think you are. For example, almost all statically typed languages since Pascal do not have value constraints but support typed enums as closed sets. There's no advanced type system needed - no need to define enums as integers and then put additional constraints in the type system to try and restrict this. There is also no need to model enums as integers in the type system in order to use integers as a runtime representation.

1 comments

You can't have closed enums without value constraints. Yes, some languages have been lazy and provided value constraints only for enum types.

Which is an interesting choice: Give a noose for developers to hang themselves with for every single other type other than enums – the types they are going to use most often – and not think twice, but then go full on helicopter parent when using enums – the one type that isn't particularly interesting.

It's a neat parlour trick, don't get me wrong, but I guess that's why almost all of the popular statically typed languages since Pascal (C, C++[1], Typescript[2], etc.) didn't bother with closed enums. They put their time into features that actually mattered to developers instead.

[1] Added later in life, granted.

[2] Ironically, does support value constraints except in the case of using enum.

> You can't have closed enums without value constraints

Sum types and closed enums don't need to constrain existing sets of values, they define the set of values. Again, I think you might be confusing the type system with runtime representation.

> It's a neat parlour trick, don't get me wrong,

It's a step towards sum types which are the mathematical dual of product types. Not a parlour trick at all, every modern language should have algebraic data types.