Open WhiteBlackGoose opened 2 years ago
Just to have my opinion documented here too, I have 3 things: about the proposed syntax
when
keyword is an odd choice to me. Most languages stich with match
, switch
or even case
.else
is the "match anything but discard it" pattern. I feel that's a bit verbose and confusing when reading it, but maybe that's just me.About features we want to support with it (numbered as you numbered them):
when
-when
feels oddWhat I'm missing:
Just a suggestion, why don't you use ;
for expressions separators? They could also be automatically inferred for any constructor that uses {}, Like:
animal when {
Frog -> green;
Cat -> {
...exprs...
}
Dogs(ds) -> for(dog in ds) {
getColor(dog);
}
}
The for
and the block don't need ;
.
So let's discuss what existing languages have, and what we want.
Potential features
1. Pattern matching over constants
This one is the simplest one. For example:
2. Pattern matching over inheritors
Checking if a given instance is a subtype of a type:
3. Deconstruction
We may allow our types to be deconstructed for pattern matching, e. g. if type is a record or just defines this method
Then we could do
4. Arbitrary condition clause
If we can't express something with pattern matching rules
5. Branch merge
We might have different cases, but in all of them we need the same value:
6. Active patterns
This is the least compile-time verified but the most functional thing, because customizeable
7. Operators on patterns
So that we could combine our conditions:
8. Conditional deconstruction
We may have deconstruction method which returns boolean. It is less powerful than active patterns, but tied to a type, not a separate function:
and
9. Equality check
Matching against non-constant instances:
10. Mixed active patterns/deconstructions and constant matching
So, we partly deconstruct, and partly match
11. Specialised operators over primitives
E. g.
12. Property matching
E. g.
What language implements what
Consider here three languages, as well as suggested features
Syntax overview
C# way
C# uses
when
for conditional clause andvar
to deconstruct. For the latter,var
makes it clear that a variable is newly declared, but also is a bit wordy.I don't like that C# needs
,
after each case. But I like that you're not forced to()
around your expression.Kotlin
else
is great. Other than that, not much to discuss since it lacks most major features of pattern matching.F
I like
|
instead of surrounding curly braces. But I don't like how wordy thismatch a with
is.What language has what
Again, these three languages + proposal for Fresh
switch
when
match ... with
when
()
needed?,
(comma)\n
(new line)\|
(pipe)=>
->
->
->
_
else
_
else
Depends*: so here I suggest single-line vs non-single:
Debates
Separator
If we don't want new line significant syntax, we will have to make cases not ambiguous. So we need a separator. Adding
pipe
makes it a bit wordy:And, in my opinion, inconsistent with the rest of the syntax. It looks like we mixed ML and Kotlin into something weird.
Should
when
be the same as other control flow constructs?may feel a bit inconsistent too.