Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
![](/static/devrant/img/pipeless-devrant-banner-white.png)
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Lensflare15889292dSome mainstream languages still don’t have enums…
I don’t want to say which because it will make some snowflakes cry. -
lorentz14940292d@Lensflare A lot of dynamically or gradually typed languages use tagged tuples, such as JS/TS and Elixir. I count that as a fat enum because it's functionally equivalent except slower, but the slowness there is a language property.
-
lorentz14940292d@ostream it's literally an integer with a type. C enums don't contribute anything to any language that has user-defined types because you can just encapsulate an int. Fat enums / tagged unions are actually doing something new.
-
lorentz14940292dC++ Variant (comparable to Java sealed interfaces) is the most plausible alternative to proper algebraic types because it forces you to declare the members individually but this is kind of a natural evolution step for enums anyway.
-
Lensflare15889292d@lorentz the main benefits of enums for me are statically checkable exhaustiveness in switch statements and discoverability of constants (autocomplete suggestions).
Of course there are also the super useful sum types (algebraic type), but the implementation via enum is a language detail (but an obvious one).
I doubt that you’d get those benefits from those dynamic typed enums which you call fat enums but I may be wrong. -
lorentz14940292d@Lensflare In Typescript you can get exhaustiveness checks but you need to explicitly do something illegal in the default branch to ensure that you're informed if it becomes reachable. Does C have exhaustiveness checks? I consider sum types a variant of enums because in a lot of languages a sum type is equivalent or even more fundamental than an enum.
-
Lensflare15889292d@lorentz yes sum types are enums in Swift and Rust, I believe. Java and Kotlin use final classes though (final having the meaning that no other subclasses are allowed)
-
Lensflare15889292d@lorentz Not sure if C has exhaustiveness checks for enums but I would assume it doesn’t.
The reason is similar to why C# doesn’t have exhaustiveness checks for enums: They are always integers and the compiler can’t guarantee that it won’t be some other integer value which is not in the the enum. -
lorentz14940292d@Lensflare Java has sealed interfaces, which specify all their implementors, thereby creating a two-way relationship which kinda works like a sum type. I think they have exhaustiveness checks for these too.
-
Lensflare15889292d@lorentz yup, they do.
Right, sealed was the word, not final. I was confusing it with Swift. -
IntrusionCM15047292dPHP 5.3 added an PECL extension, SPL, which was an attempt to add a clean standard PHP library.
Failed completely, given up in PHP 7 I think.
It contained SPLEnum and a lot of other goodies. :(
https://www.php.net/spl
Some stuff found its way in mainline....
Related Rants
I just found out that "enums" have been introduced in PHP only with v8.1 (2021).
Wtf...
Now using enums in VB6 (1998) seems magic.
rant
better late than never
php