5 ms·
> A commonly said piece of feedback from someone who's learning Rust as a second language tends to be that enums are far better supported in Rust than any other
by hurril 3y ago
> A commonly said piece of feedback from someone who's learning Rust as a second language tends to be that enums are far better supported in Rust than any other language.
This is nonsense. I doubt that this is common feedback and it is just not correct that it is far better supported in Rust than in any other language. Why would anyone say that? Rust pretty much has support for ADTs, sum types and product types. Just like any modern (functional) language has.
Haskell, Scala, F#, OCaml, etc.
(And I love rust, write it in my day-to-day as well as on my spare time.)
- ongy 3y agoBecause many people start learning in one of the industry incumbent, not necessarily good by design languages. Java, C, C++, python, Javascript, go... As a second language is a quite different selector than after education in language design or excursions to functional programming.
- pjmlp 3y agoJava enums are also quite powerful versus what C, C++ and C# can do. They are real objects, can have associated data and methods. So if they are surprised by Rust, haven't learnt Java properly
- ongy 3y agoI Haven't used java in ages. Though from a quick search, it's still just scalar/unary values. Rhough one of the better implementations of those. But the big advantage of the more sum type Style enums rust provides is the ability to nest values in them while preserving type safety guarantees. So I still see them as miles better as a language concept.
- fifticon 3y agoerh no, those in java allow you to do just that. It is one of my favorite features of java. In essence, you get to embed multiple synchronized maps inside a java enum (if you want to). All handled 'invisibly' for you.
- Ygg2 3y agoStill, you get to see all fields from all variants, while Rust variants are more isolated.
- Sharlin 3y agoNo, Java enums can contain data but do so in a manner orthogonal to Rust enums. Java enums are not sum types; each variant is simply a (`final`) instance of the enum type which, mostly, is just a regular class type with a private constructor so that no other instances can be created. All variants have the same shape and the author of the enum decides what data the variants contain. Rust enums are sum types, which means that each variant can have a totally different shape and contain different data, and it's the user of the enum that supplies the data when they create an instance of a variant. This quintessential use case for sum types not possible with Java enums (although it is now sort of possible with records and sealed traits): enum Result<T, E> { Ok(T), Err(E) }
- papercrane 3y agoJava enums are a class with a fixed number of instances. The thing in Java that is closest to Rust enum's would be sealed classes, which was a preview feature in Java 15 and finalized and released with Java 17. https://openjdk.org/jeps/409 https://openjdk.org/jeps/409
- pjmlp 3y agoStill, better than C, C++ and C#, which was my point. For the real deal, there are sealed classes now, modeled on how Scala does it.
- CodesInChaos 3y agoJava enums store the associated data once per option, while Rust enums store it per value. So they're very different. You couldn't model Rust's `Option` or `Result` as a java enum.
- pjmlp 3y agoSure, I didn't say it was the same, only they are more powerful than C, C++ and C#.
- andrybak 3y ago> You couldn't model Rust's `Option` or `Result` as a java enum. For reference, they can be modeled as a "sealed" class since Java 17. https://openjdk.org/jeps/409 https://openjdk.org/jeps/409 Prior to Java 17, sealed classes feature can be partially recreated via private constructors in the super classes. This approach forces you to put all classes in a single Java file, which can be awkward to navigate.
- pjmlp 3y agoThere is a group of folks that had learnt these features via Rust, and for whatever reason always praise Rust for "inventing" them.
- brandonmenc 3y agoA similar thing happened about 15 years ago with Ruby and “metaprogramming”.
- timeon 3y agoThis is certainly happening but one of the reasons that Rust become popular is that it included, maybe not mainstream, but already existing concepts (lifetimes included).
- Shish2k 3y ago> Haskell, Scala, F#, OCaml, etc. Rust is the first not-weirdass-academic-wtf language to have these features ;) (I love functional programming and apply the functional approach no matter what language I’m using - but why is it that functional languages are always so deeply down the academic rabbithole that they think eg “car” and "cdr” are intuitive names for “retrieve the first element of a list” and “retrieve the rest of the list” functions??)
- crvdgc 3y agoI'm sure you know car and cdr are named for historic reasons and they are not featured in ML family languages as listed above. But coming back to the name of "sum type", I find it very potent to be used with "product type" to reason about Algebraic Data Type's properties, like a * (b + c) = a * b + a * c and don't understand why it's often avoided to be called in this way in "mainstream" programming languages.
- aeonik 3y ago`car` (Contents of Address part of Register) and `cdr` (Contents of Decrement part of Register) are historical terms from the IBM 704, used in LISP. They're legacy from early computing, not academic jargon.
- kazinator 3y agoMacCarthy transformed them into academic jargon in fact. Knowing that the words originated in the IBM 704 idiosyncrasies not found in other machines, he used them anyway in papers about symbolic computation. Lisp pairs are flexible objects that can be coupled together into shapes and uses that are not lists. car and cdr can mean first and rest, and ANSI Common Lisp [1994] has those synonyms, but there are uses of cons cells where first and rest do not make sense. MacCarthy must have realized that words that do not invoke any connotations are good for the elements of a flexible pair structure. Choices like first/rest, left/right, top/bottom and others are saddled with semantics that don't match every use. Programs that need a pair structure in which the two pieces have very specific roles can provide their own synonyms, if their authors feel it makes code more readable. I seem to recall that Knuth, in TAOCP, at one point, calls the pointers of a binary tree node ALINK and BLINK.
- lawn 3y agoObvious they would say that because they're not familiar with the other languages. Like it or not they probably come from Python/C++/JavaScript or similar.
- azzentys 3y agoI think that's one of the important features why I LOVE Rust! I recently wrote a Chip-8 emulator and creating an enum with all commands = AMAZING! (I come from C/Embedded world)
- nfriedly 3y agoI might say something like that. My background is a lot of JavaScript and a little bit of other things like c++, python, Ruby, php, etc. To me, enums are one of the great things about Rust, and I wish I had the same capabilities in JavaScript and other languages.
- golergka 3y agoThe author talks about people who learn it as a second language. Not third or tenth.
- chlorion 3y ago>Why would anyone say that? Most people have not had any exposure to functional programming languages or functional programming in general. When they say "any other language" they mean, any other language that they have used or know anything about I guess. This is one of those cases where we fail to realize that other people's reality is totally different than ours. Everyone does this all of the time, not just you specifically to be clear, this is a limitation in our brains.