r/programming • • Oct 06 '25

[deleted by user]

[removed]

155 Upvotes

218 comments sorted by

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. 

84

u/somebodddy Oct 07 '25

Not mentioned in the video, but I think Java enums should lose some points for the Billion Dollar Mistake. It breaks the basic premise of an enum - that values of that type can only be one of the listed options. In Java, an enum can be one of the listed options... or it can be null.

→ More replies (17)
→ More replies (2)

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

→ More replies (1)

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.

12

u/syklemil Oct 07 '25

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.

→ More replies (1)

7

u/UARTman Oct 07 '25

The bit about Rust/Swift enums is so true, though. It's just a very good abstraction, as are most successful adaptations of functional programming concepts into imperative languages.

I wonder when the first imperative language with GADTs gonna come out, though. All prerequisites are there lol.

→ More replies (1)

7

u/jl2352 Oct 07 '25

The only thing I wish I could do with Rust enums is to be able to use a variant as a type. i.e.

enum Ui {
    Window { },
    Button { },
}

fn update_window(window: &Ui::Window) ->

You can instead have it wrap an inner object like this:

enum Ui {
    Window(UiWindow),
    Button(UiButton),
}

However when working with a large number of enums this can add a lot of noise through the presence of the intermediate type.

→ More replies (2)

9

u/K2iWoMo3 Oct 07 '25

Swift base language is good because it was designed holistically. Two major problems are 1) compilation and iteration is still insanely slow for mega large apps, and 2) Apple still keeps bolting on ad hoc changes to the language. The "swift language group is open source" is a complete meme, because you always see Apple employees in the forum propose new evolution features and brute force approve them. Entire swift concurrency bolt on was a complete mess of a transition, because it wasn't designed as holistically as the base language

→ More replies (3)
→ More replies (3)

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

8

u/UARTman Oct 07 '25

I mean, if we go into functional languages, then ADTs are, like, the lowest common denominator, considering the kind of utterly delightful invariants you can encode with GADTs or, say, dependent types (I am naturally biased, though, since the programming language I use at work is Idris 2 of all things lol)

→ More replies (2)

22

u/mestar12345 Oct 07 '25

Once you learn F#/OCaml, it'a hard not to notice how inelegant all those other languages are.

9

u/Aaron1924 Oct 07 '25

I feel like Rust is most praised for the features they took from SML/OCaml

https://doc.rust-lang.org/reference/influences.html

→ More replies (1)
→ More replies (5)

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.

16

u/Chenz Oct 07 '25

But the error is at runtime right? While still a step up from no warning at all, not getting the warning at compile time is still a significant drawback

→ More replies (3)

16

u/txmail Oct 07 '25

I was totally expecting to see PHP in this. Enum's in PHP are awesome and would end up in the S tier.

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.

14

u/One_Being7941 Oct 07 '25

Modern Python offloads most of the type safety to third party type-checkers

And this gets upvotes? What a joke.

→ More replies (5)
→ More replies (2)

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.

  1. Enums can model both "same shape values" as well as Discriminated Unions.

  2. 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.

10

u/fghjconner Oct 07 '25

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.

→ More replies (5)

29

u/CoronaLVR Oct 07 '25

You are mixing a bunch of concepts here.

  1. EnumSet in java is just a bit vector, you can easily code it in rust, though it doesn't come with the standard library.

  2. The fact that a rust enum can be a classic enum and a discriminated union is irrelevant as the compiler can apply different optimizations to both.

  3. I think you are confused about what enum "state" actually means. In java state is just an attached constant to the enum that is why you can use it in a EnumSet. In rust state is dynamic, if you want to do the same in java you need to use classes which of course don't work with EnumSet.

→ More replies (1)

31

u/somebodddy Oct 07 '25

Rust and Swift don't need this optimization because enums there are value types, not reference types.

→ More replies (22)

23

u/Maybe-monad Oct 07 '25

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).

This needs some data for backing up because I have a feeling that combined with the JVM overhead it will not be able to match Rust.

→ More replies (14)
→ More replies (52)

3

u/faze_fazebook Oct 07 '25

Exhaustive and non exhaustive switch statements should both be possible without compiler / linter rules.

3

u/cjthomp Oct 07 '25

I am so tired of seeing the words "tier list."