New Tagged/discriminated enums and new match operator for pattern matching #242
Replies: 3 comments 2 replies
-
Yes, I have thoughts about adding tagged unions at some point. Though note:
That is to say, people tend to have a lot of excitement for tagged unions but are not actually aware why they want them (over object with inheritance). Personally, my biggest interest from the above would be in performance, I am guessing for you it is pattern matching? What if Lobster had pattern matching on objects, would you still want them? Anyway, if I'd add them to Lobster, they'd likely piggy-back on
Or maybe we could invent a more convenient syntax to all declare that in 1, but that is immaterial to the concept. Then, an instance of If you look in
|
Beta Was this translation helpful? Give feedback.
-
Oh and as for your note on exceptions: Lobster does not have language-native exceptions. It does however have them implemented in terms of non-local returns in |
Beta Was this translation helpful? Give feedback.
-
For now, we have exhaustive switch on types, which goes pretty far towards this functionality, is simple and fast! Example: lobster/tests/typeswitch.lobster Lines 6 to 77 in b74f00c Docs: lobster/docs/source/language_reference.md Lines 727 to 750 in b74f00c |
Beta Was this translation helpful? Give feedback.
-
Hi, just learned about Lobster on Youtube. Big fan of the simplicity and syntax, congratulations!
The proposal below assumes you answer yes to the question "Can you/do you want to implement it". Feel free to totally ditch it if it's not the case!
Tagged enum - typed constructor
We could define enums cases to accept constructors:
Tagged enum - with generic parameter
Why
I think lobster did an an amazing job at implementing type safety in a simple manner. I always wondered through different statically analyzed languages (F#, Haskell, V, Zig, ...) and every time I tried to put my head in those, I always stumble upon a "what the freck" moment about either the syntax, or the poor tagged union capability/support (to be completely fair, F# was the closest to my definition of a good and simple language).
I think lobster can definitely differenciate itself by implement true tagged union and pattern matching ability. Languages have always wanted to draw a line between FP and OOP, and Lobster can join them both like a good old Lord of the Ring movie.
More seriously, in my opinion having Tagged union and pattern matching would increase the robustness of the language because this way of describing a program is way much safer since the programmer that use your module not only handle the "happy path" but also all the possible "exception".
End note
This proposal will have no sense if you tell me the compiler is able to statically warn if a code that could throw is not properly try-catched.
The big show stopper about languages implementing throw mecanism is mostly the inability to be able to detect unhandled possible exceptions thrown by a function. If Lobster is able to detect this, then this proposal would have a lower interest for me.
Beta Was this translation helpful? Give feedback.
All reactions