-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Pattern matching #44
Labels
Language idea
One single idea which may be a start of a design document
Syntax
This issue is about syntax
Comments
WhiteBlackGoose
added
Language idea
One single idea which may be a start of a design document
Syntax
This issue is about syntax
labels
Jun 16, 2022
Just to have my opinion documented here too, I have 3 things: about the proposed syntax
About features we want to support with it (numbered as you numbered them):
What I'm missing:
|
Just a suggestion, why don't you use
The |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Language idea
One single idea which may be a start of a design document
Syntax
This issue is about syntax
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.
The text was updated successfully, but these errors were encountered: