|
|
|
|
|
by andyferris
899 days ago
|
|
One thing I like is exploring the structural typing. However I am confused if the enum's are structural or nominal. It seems necessary to state which enum a variant comes from, like `Direction of South` (for that matter it seems the same is true when constructing structs?). Can you cast enum values to a wider enum type? Or combine different enum values (e.g. through branching - the return of `match`) to construct a value of a wider enum? |
|
``` enum Option<T> {Some(T), Nothing}
// Create a value with an inline type that doesn't use the nominal `Option`
let opt1 = enum {Some(Int), Nothing} of Some(5);
// Define a second option value, which is assigned with the first
let opt2: Option<Int> = opt1;
// Print the result
println(opt2); ```
You can create a variant of an inline `enum` type, and then typecheck it against another structurally equal type if you so choose!
I hope this example properly illustrates how powerful the structural type-checking is with enums!