I kind of consider algebraic data types (the combination of this style of enum (sum types) and structs (product types)) paired with pattern matching as the core feature set that any modern language must support. It makes for really simple, easy to follow code that’s much less error prone than the alternatives.
Using enums to represent state in Rust
11–20 of 89 posts
Re: Using enums to represent state in Rust
#12The next step is to encode your transition logic in the From impls between the enum structs and you've got yourself a first-rate state machine.
Re: Using enums to represent state in Rust
#13What does this look like under the hood (in memory)? Does the compiler automatically generate a struct/union? Does the value take up the same width regardless of state?
Re: Using enums to represent state in Rust
#14> Deleted { deleted_at: DateTime }, What does this look like under the hood (in memory)? Does the compiler automatically generate a struct/union? Does the value take up the same width regardless of state?
Re: Using enums to represent state in Rust
#15> Deleted { deleted_at: DateTime }, What does this look like under the hood (in memory)? Does the compiler automatically generate a struct/union? Does the value take up the same width regardless of state?
Re: Using enums to represent state in Rust
#16Re: Using enums to represent state in Rust
#17I kind of consider algebraic data types (the combination of this style of enum (sum types) and structs (product types)) paired with pattern matching as the core feature set that any modern language must support. It makes for really simple, easy to follow code that’s much less error prone than the alternatives.
I just wish Rust wouldn't have called those things 'enums' but 'tagged unions', would have saved us C peasants a lot of confusion when encountering them first ;)
Being able to say 'Rust enums can carry arguments' - for some reason - sounds less intimidating and conveys the core feature.
Re: Using enums to represent state in Rust
#18Earlier quoted context omitted.
I just wish Rust wouldn't have called those things 'enums' but 'tagged unions', would have saved us C peasants a lot of confusion when encountering them first ;)
C was my first language, and I actually have the opposite opinion - I suspect that calling them 'enums' helps adoption. My reasoning is that phrases like 'tagged unions' and 'algebraic datatypes' strike some developers as sounding very academic/ivory tower and so somewhat intimidating. I think this is really unfortunate (and is definitely not universal among developers). Being able to say 'Rust enums can carry argume…
(but yeah, 'enum with arguments' also describes it very well)
Re: Using enums to represent state in Rust
#19Rust ADTs and pattern matching are so much better than other mainstream languages I find that once my code compiles it actually is almost always correct. The next step is to encode your transition logic in the From impls between the enum structs and you've got yourself a first-rate state machine.
Pascal and Delphi have always had variant records as part of the language. Are these not "mainstream" enough, especially Delphi?
Re: Using enums to represent state in Rust
#20I kind of consider algebraic data types (the combination of this style of enum (sum types) and structs (product types)) paired with pattern matching as the core feature set that any modern language must support. It makes for really simple, easy to follow code that’s much less error prone than the alternatives.
I just wish Rust wouldn't have called those things 'enums' but 'tagged unions', would have saved us C peasants a lot of confusion when encountering them first ;)