Algebraic data types and structural pattern matching RFC

2023-05-23 Thread j-james
Hmm, `if circ as Circle(binding):` seems nice.

Algebraic data types and structural pattern matching RFC

2023-05-23 Thread ElegantBeef
`circl as Circle` is possible to being implemented if I recall correctly. The one downside is that you cannot do `var circ: Circle` could even do `if shape as Circle(circ)` or similar. Given that j-james has suggested just binding the mutability to the symbol type the `as` syntax could be a fine

Algebraic data types and structural pattern matching RFC

2023-05-23 Thread elcritch
Yah Rusts `if let …` just feels so ugly to me. Perhaps something like `if circ as Circle:` or `if circ into Circle:`. I think those aren’t possible to implement as a library feature though IIRC. Just overshadowing the name simplifies the syntax a lot. Maybe even an operator if it was standardiz

Algebraic data types and structural pattern matching RFC

2023-05-23 Thread mratsim
The word unions / tagged unions / discriminated unions have a standard meaning in computer science. It's fine because with context it's hard to confuse the two. Using `modulo` for `remainder` is a way worse offense because both are used in the same context. For example the term `field` has a v

Algebraic data types and structural pattern matching RFC

2023-05-23 Thread PMunch
@j-james, fixed the paste issue

Algebraic data types and structural pattern matching RFC

2023-05-23 Thread xigoi
The word “union” has a standard meaning in mathematics. And is there any language other than Rust that calls sum types “enums”? Yeah, `sum` would be bad as a keyword. Perhaps `variant`?

Algebraic data types and structural pattern matching RFC

2023-05-23 Thread j-james
I would argue with you there: computer science terminology is far from standardized (just look at dynamic arrays! every language uses a totally different name). I would consider the implementation of this to be a tagged union, and in type theory `enum` is a standard synonym for sum types. I've

Algebraic data types and structural pattern matching RFC

2023-05-23 Thread j-james
Yeah, I'd really like an equivalent of Rust's `if let`: but **not** `if let`! I hate the syntax there too :-D Definitely taking suggestions there (and everywhere, but especially there). My current thought is `if Circle(binding) == variable` but I don't really like it either: it's clean but very

Algebraic data types and structural pattern matching RFC

2023-05-22 Thread elcritch
> What you think if (circ: Circle) from shape is hacky?! It's hardly readable! > :P Never! :sweat: ;)

Algebraic data types and structural pattern matching RFC

2023-05-22 Thread ElegantBeef
@J-James has resolved the var/non-var by just using the mutability of the variable, if you match on a mutable variable it's mutable, and same for immutable. > There's some ways to one line if/else matches but they're all kinda hacky. What you think `if (circ: Circle) from shape` is hacky?! It's

Algebraic data types and structural pattern matching RFC

2023-05-22 Thread elcritch
I believe some of the work @ElegantBeef has done with [fungus](https://github.com/beef331/fungus/tree/commonfields) is useful to consider. In particular, how to handle var vs non-var matches? Also, having a conical if/else idiom would be useful. There's some ways to one line if/else matches but

Algebraic data types and structural pattern matching RFC

2023-05-22 Thread xigoi
The keyword conveys that it's a union type, which is false. `enum` would be even worse since it would imply that it's an enumerated type.

Algebraic data types and structural pattern matching RFC

2023-05-21 Thread j-james
The types of the variants are not required to be unique. While I somewhat agree from a type theory perspective (well, actually, I'd prefer this to be called `enum` and objects to be called `struct`), I think `union` is a much more approachable keyword that conveys what the type is doing better.

Algebraic data types and structural pattern matching RFC

2023-05-21 Thread xigoi
Are the types of the variants required to be unique? If not, it should be called `sum` rather than `union`.

Algebraic data types and structural pattern matching RFC

2023-05-21 Thread j-james
It seems "unnatural" because it is complete garbage! Middle-click paste strikes again, that is the same code block accidentally pasted into itself. I can't seem to edit my post any more as it's too old, shame. If you have post edit privileges would you mind removing lines 13 through 24 of the co

Algebraic data types and structural pattern matching RFC

2023-05-21 Thread mratsim
Very interesting, actually when I was using Rust I kind of missed Haskell pattern matching. Regarding this type Term = union Unittype Ident = string Run This seems "unnatural", a bit similar to Rust Phantom data types, I don't see this syntax in the RFC either,

Algebraic data types and structural pattern matching RFC

2023-05-21 Thread alexeypetrushin
+1 for Union, in 80% of cases when I use Variant, Union would be better. And of course, Union is useless without auto-cast, so structural (or some other auto-cast) is a must.

Algebraic data types and structural pattern matching RFC

2023-05-21 Thread j-james
Hello, I have created an RFC for proper algebraic data types and structural pattern matching a la Swift or Rust. It's available here: I go into much more detail on the RFC itself, but here is an example of code it would allow you to write: