3 ms·
> "can't pattern match if something-something pointers" The main confusion I see is that in many languages it is unclear whether sum types are a runtime or com
by jerome-jh 6y ago
> "can't pattern match if something-something pointers"
The main confusion I see is that in many languages it is unclear whether sum types are a runtime or compile time thing, which decides whether pattern matching is semantic or syntactic. Of course nobody wonders whether "if then else" is semantic, but for pattern matching I admit I am myself confused:
- in C all types are compile time (and loosely enforced) so if you want sum types, do them yourself. Implementing the matching is quite straightforward but unsafe and pedantic.
- in Java all type information is available at runtime: sum types can be implemented easily (casting to Object), matching is safe and can be compact if done with e.g. arrays.
- in C++ thinks start getting trickier: you can certainly do a lot mixing static and dynamic casts, but will this remain readable?
- in ML languages: AFAIK this is completely implementation defined. The compiler may choose to box some or all variables. Obviously in such a language you much less care about what is going on under the hood. In return you get a true pattern-matching construct.
- in Rust and others: frankly I don't know where they stand in regard of compile time vs runtime :)