-
Notifications
You must be signed in to change notification settings - Fork 89
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
The Syntax Bikeshedding Dojo, round 8: switch #817
Comments
I think just |
Is it possible to have the switch statement be more like a normal function that takes in a value and a record? Kind of like in Nix we use
Sorry if this is outside the scope of this proposal and / or doesn't make sense. Feel free to ignore. |
I like the word We have a number of alternatives for the grammar (this is orthogonal to switch vs match vs case).
I'm not sure what I like best. |
But the question is, is meant to stay that way? I feel like if we have a syntax for destructuring pattern, we may introduce pattern matching as well (although I'm well aware that while it's natural on the user side, the implementation of (efficient) pattern matching is more complex than plain destructuring)
Yes. My personal, unscientific and unsubstantiated impression is that in functional languages you often match on the result of an application, so this is a tad annoying. That being said, if we stick to a switch-for-enum and not pattern matching, then it may be different.
@arobertn indeed mentioned elsewhere that Scala does this (pattern matching in Scala). This also looks reasonable. |
Right now the syntax for switch cases and a normal record is already different, but this is just an arbitrary choice. However if we are to add pattern matching then it becomes obviously impossible. |
Random thoughts and remarks, even though I'm late to the party 😅 Current syntaxI agree with @yannham that the current syntax
decently easy to read. Switch vs pattern matching
I concur with @yannham : pattern matching is strictly more powerful than switching, so if we stick with Switching/Pattern matching as a functionI (think I) partially agree with @Radvendii 's idea/suggestion: IMO a switch or pattern matching can always be considered as a function taking the expression to match on and returning a value, right?
should be a valid Nickel expression: it's a function. Do we even need a keyword?I am not advocating to remove the keyword, be it
Note: I am not arguing that the keyword-less syntax for switch/pattern matching should be |
We had another discussion about this. The idea of being able to "curry" pattern matching (asked a long time ago in #477) is popular, to allow users o define functions matching on their argument in a direct and natural way. Together with the reverse application pipe operator, we can achieve something similar to Scala, introducing just one key word (
But would also be able to match inline by just relying on reverse application:
[EDIT: MISSING PIPE FIXED] |
Is |
Oh, right, the revapp pipe operator is missing:
|
Ah, makes sense. And why do you need
|
It's just for the sake of having an example of matching on a function application (making the point that you don't need parentheses), but it's indeed useless semantically 🙂 |
Ah, the point is that application binds tighter than |
Proposal implemented in #970 |
Switch
We have an unloved construct in Nickel, the switch. It's currently not as useful as it could be, since it's just a C-like switch that works only on enum tags. However, it could become a nice tool in the future, if we allow to match against patterns, literals, etc.
Issues
However, there are a few quirks:
The current syntax is odd: we first write the cases, and only then the expression that is matched. This structure makes the flow hard to follow, in my humble opinion:
switch
is reminiscent of C or JavaScript (to be fair, it is currently close to those operators in semantics), but isn't common in functional languages with pattern matching. If we are to implement pattern matching, we should probably use a different keyword.Proposals
Note that because of the ML-y syntax of Nickel, we can't just swap the order of the cases and of the argument. Otherwise, we can't easily disambiguate between a record that is an argument to a function application and the cases of a switch (well we can right now, but probably won't be able in the future using an LR(1) parser without hacky backtracking, eg if we allow to match on string literal or to bind simple variables like "x") :
One common way to disambiguate is to add a keyword between the expression and the cases. For example:
with
:match ... with ...
of
:cases ... of ...
I think using either of the two
match ... with ...
orcase ... of ...
is reasonable. C-like languages seems to have stuck with the switch (Java, TypeScript for the compatibility with JavaScript, C#, etc.), while elsewhere thematch
seems to be popular (Python, Rust, Racket, etc.).case ... of ...
looks quite specific to Haskell-y or ML-y languages.Another solution for disambiguation is to use a keyword for cases that makes a clear distinction with record literal, for example:
However we pay one additional keyword for each case, while the previous solution is just one word for the whole pattern match.
The text was updated successfully, but these errors were encountered: