5
JS96
1y

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.

Comments
  • 4
    Some mainstream languages still don’t have enums…

    I don’t want to say which because it will make some snowflakes cry.
  • 2
    And it's the shitty kind too
  • 0
    @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.
  • 2
    @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.
  • 0
    C++ 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.
  • 1
    @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.
  • 1
    @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.
  • 0
    @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)
  • 1
    @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.
  • 1
    @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.
  • 0
    @lorentz yup, they do.
    Right, sealed was the word, not final. I was confusing it with Swift.
  • 1
    PHP 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....
Add Comment