Hacker News new | past | comments | ask | show | jobs | submit login

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?




Hello, thanks for the great question! Yes, you do have to specify where a variant comes from, but it doesn't have to be nominal at all! Here's an example:

``` 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!


That's an interesting approach. So the variant name itself doesn't bear the type information but should be uniquely resolved in the type check time, right? It will be pretty much optimal unless you are doing metaprogramming stuffs.


That's right! I think it's a good balance between giving the typechecker the info it needs, but also being flexible in how you can supply it!


I once pondered about a related generalization: an explicit ellipsis in the identifier. For example `*foo` will match any suitable identifier that ends with `foo`, and it should be uniquely resolved at the end. More ergonomic syntax would use a separator to imply wildcards: for example, an identifier should be a sequence of words separated by `-`, so that `-foo`, `foo-` and `-foo-` really mean `*-foo`, `foo-*` and `*-foo-*` respectively. This can be used to implement a simpler form of module paths.


Fascinating.

I'm imagining you're going to want a kick-ass IDE/linter/VSCode plugin to handle the case when you create a new variable that matches an existing pattern, and automatically making existing references more precise.

Though it might be easier to go the other way - at the presentation layer, automatically shorten variables to the least specific yet unique suffix or something.


Oh that's cool, and makes sense. Is it possible to perform type widening on enums? Or must they match precisely? For example, what happens if I attempt the below?

``` enum Option<T> {Some(T), Nothing};

let opt1 = enum {Some(Int)} of Some(5);

let opt2: Option<Int> = opt1; ```




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: