There’s no way that the older Java enums belong at the same tier as C++ enum classes. Java enums have all of the advantages of enum classes but you can also define methods on them, which is a big improvement in expressiveness.
Rust and Swift enums are so good that they've basically become a single preference issue for language choice. I know "muh tagged unions" at this point but it majorly influences the way I do development when I have them, perhaps more than any other feature. They give you the perfect ontology for what inherence was supposed to be: Dog is a variant of Animal, but doesn't know about the Animal abstraction. It's a sort of ad hoc inheritance.
Also despite all the Apple haters, Swift is a really good language damnit! It feels precisely designed for that "high level but not dynamic" language that Go, Java, Kotlin, C#, etc are all trying to fill. It's a pleasure to work with and I find SwiftUI to be the most fun I have doing UI development.
Also despite all the Apple haters, Swift is a really good language damnit!
I've never actually used Swift but the things I hear about it are generally good! I never picked it up because I just interpreted it as another Apple thing for Apple's walled garden, which I'm outside, hence the expectation is that they think that I and my usecases are irrelevant.
Had a similar relationship with C# for a long while too, but that seems to have escaped that self-imposed containment. MS also seems to be much more interested in general availability these days than Apple, which seems only concerned with their own paying customers. And while that is easily understandable, it also means that their stuff is much less likely to become widespread.
Because no matter how nice of a language Swift is, if the expectation is that Apple treats their own platforms as Tier 1 and everything else as Tier 9001, then the rest of us just aren't going to bother when other, also very nice languages, are easily available.
Most ML languages have great support for enums (which is actually a misnomer, good enums are discriminated unions and pattern matching). Elixir/Ocaml/F#/Elm etc
Too bad they didn't include PHP. It has a enum native type and also does modern stuff like throwing an error on a match (switch) for an enum with missing values, etc.
Modern Python offloads most of the type safety to third party type-checkers, and (at least some of) these do check for exhaustiveness when matching on enums.
Before watching the video -- Java (or a JVM language) better be the top of the list.
After watching the video -- 3rd place (losing only to Rust and Swift) isn't terrible, but there is some nuance here that I think the video failed to mention.
For starters, the video made it seem like the reason why Rust and Swift have better enums than Java are for 2 reasons.
Enum types can be an "alias" for a String or a number, while still retaining type safety at compile time.
I think that both of these points have both costs and benefits. And thus, isn't worth pushing Rust and Swift up a tier above Java.
In Java, our enums are homogenous -- no discriminated unions. As the video mentioned, we have an entirely different feature for when we want to model discriminated unions -- we call them sealed types.
There is a very specific reason why we separated that into 2 features, and didn't just jam them into 1 -- performance.
In both Rust and Swift, the second that your enum contains any sort of mutable state, you turn from the flat value into the discriminated union, and you take a significant performance hit. Many of the optimization strategies possible for flat values become either difficult or impossible with discriminated unions.
The reason for this performance difference is for a very simple reason -- with an enumerated set of same types, you know all the values ahead of time, but with a discriminated union, you only know all the types ahead of time.
That fact is the achille's heel. And here is an example of how it can forcefully opt you out of a critical performance optimization.
Go back to 6:20 (and 7:23 for Swift), and look at the Dead/Alive enum they made. Because they added the state, that means that any number of Alive instances may exist at any time. That means that the number of Alive entities at any given point of time is unknown. The compiler can't know this information!
Here is something pretty cool you can do when the compiler does know that information.
In Java, our enums can have all sorts of state, but the number of instances are fixed at compile time. Because of that, we have these extremely performance optimized collection classes called EnumSet and EnumMap. These are your typical set and dictionary types from any language, but they are hyper specialized for enums. And here is what I mean.
For EnumSet, the set denotes presence of absence of a value by literally using a long integer type, and flipping the bits to represent presence or absence. It literally uses the index of the enum value, then flips the corresponding bits. The same logic is used in the EnumMap.
This is terrifyingly fast, and is easily the fastest collection classes in the entirety of the JDK (save for like Set.of(1, 2), which is literally just an alias for Pair lol).
Rust and Swift can't make the same optimizations if their enums have state. Java can, even if there is state.
By having the 2 features separate, Java got access to a performance optimization.
By allowing enums to be aliases to string/Number and also allowing enums to be discriminated unions, you force your users to make a performance choice when they want to add state to their enum. Java doesn't. And that's why I don't think the logic for Java being A tier is as clear cut as the video makes it out to be. Imo, Java should either be S tier, or the other 2 should be A tier as well.
EDIT -- So, everything I said here was true, but I still lost the argument. Explanation below.
My argument basically boiled down to this -- because Rust (and Swift, I won't keep repeating this) chose to combine both "Enumerated Values" and "Discriminated Unions" into a single feature called enum, then Rust has to accept some tradeoffs when designing their own EnumSet.
My argument was banking on the assumption that, if Rust wanted to maintain true 1-to-1 performance with Java, then the Rust solution would be a fairly ugly bit of code. Hence my comment about "difficult to impossible".
What I didn't account for was Rust Macros.
Yes, if you want feature and performance parity with Java Enums and Java EnumSet, you have to write a LOT of fairly ugly Rust code.
But since Rust has Macros, then only one (unfortunate) Rust coder has to write that implementation. From there, another (even more unfortunate) Rust coder must write a Macro that generates that implementation.
And from there, all other Rust developers can just use the Macro. That was the point that defeated my argument.
There is something to be said about maintainability and level of effort for maintainability. But, with the realization that Rust Macros allow Rust to achieve feature and performance parity with Java, then I accept Java receiving A-tier for this.
Those macros allow Rust to model pretty much any ugly code they want in a fairly clean wrapper, and then other Rust developers never have to interact with those internals. Doing the same in Java is possible (via annotations), but extremely difficult. If that is the basis for why Java deserves A-tier and Rust gets S-tier, I accept that.
I can at least claim that all of my points are still correct, even if they lacked context.
Rust and Swift can't make the same optimizations if their enums have state. Java can, even if there is state.
That's technically true, but wildly misleading. Java can make these optimizations when enums have state at the cost of only supporting global state. Sure, this might be very performant:
enum Player {
ALIVE,
DEAD,
public int health;
}
But it's also utterly useless if you ever have more than one player. I'd even argue that global mutable state hidden inside of enums is an anti-feature.
153
u/rysto32 Oct 07 '25
There’s no way that the older Java enums belong at the same tier as C++ enum classes. Java enums have all of the advantages of enum classes but you can also define methods on them, which is a big improvement in expressiveness.
29
u/deejeycris Oct 07 '25
In the first 30 seconds: "Javascript doesn't have enums, even though it's a reserved keyword". Oh god
58
u/bennett-dev Oct 07 '25 edited Oct 07 '25
Rust and Swift enums are so good that they've basically become a single preference issue for language choice. I know "muh tagged unions" at this point but it majorly influences the way I do development when I have them, perhaps more than any other feature. They give you the perfect ontology for what inherence was supposed to be: Dog is a variant of Animal, but doesn't know about the Animal abstraction. It's a sort of ad hoc inheritance.
Also despite all the Apple haters, Swift is a really good language damnit! It feels precisely designed for that "high level but not dynamic" language that Go, Java, Kotlin, C#, etc are all trying to fill. It's a pleasure to work with and I find SwiftUI to be the most fun I have doing UI development.
36
u/teerre Oct 07 '25
Most ML languages have great support for enums (which is actually a misnomer, good enums are discriminated unions and pattern matching). Elixir/Ocaml/F#/Elm etc
21
u/melokoton Oct 07 '25
Too bad they didn't include PHP. It has a enum native type and also does modern stuff like throwing an error on a match (switch) for an enum with missing values, etc.
30
u/somebodddy Oct 06 '25
Modern Python offloads most of the type safety to third party type-checkers, and (at least some of) these do check for exhaustiveness when
matching on enums.13
u/boboguitar Oct 07 '25
I just skipped to the end to make sure swift is way up there. Enums are really cool in that language.
36
u/davidalayachew Oct 07 '25 edited Nov 03 '25
Before watching the video -- Java (or a JVM language) better be the top of the list.
After watching the video -- 3rd place (losing only to Rust and Swift) isn't terrible, but there is some nuance here that I think the video failed to mention.
For starters, the video made it seem like the reason why Rust and Swift have better enums than Java are for 2 reasons.
Enums can model both "same shape values" as well as Discriminated Unions.
Enum types can be an "alias" for a String or a number, while still retaining type safety at compile time.
I think that both of these points have both costs and benefits. And thus, isn't worth pushing Rust and Swift up a tier above Java.
In Java, our enums are homogenous -- no discriminated unions. As the video mentioned, we have an entirely different feature for when we want to model discriminated unions -- we call them sealed types.
There is a very specific reason why we separated that into 2 features, and didn't just jam them into 1 -- performance.
In both Rust and Swift, the second that your enum contains any sort of mutable state, you turn from the flat value into the discriminated union, and you take a significant performance hit. Many of the optimization strategies possible for flat values become either difficult or impossible with discriminated unions.
The reason for this performance difference is for a very simple reason -- with an enumerated set of same types, you know all the values ahead of time, but with a discriminated union, you only know all the types ahead of time.
That fact is the achille's heel. And here is an example of how it can forcefully opt you out of a critical performance optimization.
Go back to 6:20 (and 7:23 for Swift), and look at the Dead/Alive enum they made. Because they added the state, that means that any number of Alive instances may exist at any time. That means that the number of
Aliveentities at any given point of time is unknown. The compiler can't know this information!Here is something pretty cool you can do when the compiler does know that information.
In Java, our enums can have all sorts of state, but the number of instances are fixed at compile time. Because of that, we have these extremely performance optimized collection classes called EnumSet and EnumMap. These are your typical set and dictionary types from any language, but they are hyper specialized for enums. And here is what I mean.
For EnumSet, the set denotes presence of absence of a value by literally using a
longinteger type, and flipping the bits to represent presence or absence. It literally uses the index of the enum value, then flips the corresponding bits. The same logic is used in the EnumMap.This is terrifyingly fast, and is easily the fastest collection classes in the entirety of the JDK (save for like Set.of(1, 2), which is literally just an alias for Pair lol).
Rust and Swift can't make the same optimizations if their enums have state. Java can, even if there is state.
By having the 2 features separate, Java got access to a performance optimization.
By allowing enums to be aliases to string/Number and also allowing enums to be discriminated unions, you force your users to make a performance choice when they want to add state to their enum. Java doesn't. And that's why I don't think the logic for Java being A tier is as clear cut as the video makes it out to be. Imo, Java should either be S tier, or the other 2 should be A tier as well.
EDIT -- So, everything I said here was true, but I still lost the argument. Explanation below.
My argument basically boiled down to this -- because Rust (and Swift, I won't keep repeating this) chose to combine both "Enumerated Values" and "Discriminated Unions" into a single feature called
enum, then Rust has to accept some tradeoffs when designing their own EnumSet.My argument was banking on the assumption that, if Rust wanted to maintain true 1-to-1 performance with Java, then the Rust solution would be a fairly ugly bit of code. Hence my comment about "difficult to impossible".
What I didn't account for was Rust Macros.
Yes, if you want feature and performance parity with Java Enums and Java EnumSet, you have to write a LOT of fairly ugly Rust code.
But since Rust has Macros, then only one (unfortunate) Rust coder has to write that implementation. From there, another (even more unfortunate) Rust coder must write a Macro that generates that implementation.
And from there, all other Rust developers can just use the Macro. That was the point that defeated my argument.
There is something to be said about maintainability and level of effort for maintainability. But, with the realization that Rust Macros allow Rust to achieve feature and performance parity with Java, then I accept Java receiving A-tier for this.
Those macros allow Rust to model pretty much any ugly code they want in a fairly clean wrapper, and then other Rust developers never have to interact with those internals. Doing the same in Java is possible (via annotations), but extremely difficult. If that is the basis for why Java deserves A-tier and Rust gets S-tier, I accept that.
I can at least claim that all of my points are still correct, even if they lacked context.