4 ms·
I work on Dart which is also adding pattern matching [1]. When we designed the syntax for guards, we also considered exactly these three choices before ultimate
by munificent 4y ago
I work on Dart which is also adding pattern matching [1]. When we designed the syntax for guards, we also considered exactly these three choices before ultimately landing on "when" too.
Our main reasoning was:
"&&" is intuitive but it means that you can have a pattern that is immediately followed by an infix operator. That can be problematic if you ever want to make "&&" a valid pattern infix operator. And, in our case, we ended up doing exactly that, so "&&" would have been ambiguous. If you were to write:
case foo && true:
Then it could be parsed as either a pattern that matches when the value is equal to the constant "foo" and is equal to the constant "true". Or it could be parsed as a pattern that matches when the value is equal to the constant "foo" followed by a pointless guard that always succeeds.
"if" is nice because it's already a reserved word and the semantics are pretty obvious. But in Dart, an if statement always has the condition in parentheses. Those are pointless in a pattern since we already have another explicit delimiter separating the guard from the case body:
case foo if (condition):
// ^
We could say that the if condition in a guard doesn't need parentheses but if conditions elsewhere do. But that's likely just an annoying footgun where users will write them unnecessarily (but harmelessly at least) in guards and forget them in if statements and get compile errors.
If Dart didn't require parentheses around if conditions, we probably would have used "if" for guard clauses to (like Scala and Rust do).
So we tried "when" and most users and team members seem to like it. Syntax design is a human-centered process so often the right answer is just what feels right to the most people.
[1]: https://github.com/dart-lang/language/blob/master/accepted/future-releases/0546-patterns/feature-specification.md https://github.com/dart-lang/language/blob/master/accepted/f...
- Pet_Ant 4y agoGotta say I'd've taken the if with parentheses myself. Feels like it's reusing something familiar even if they are extraneous at times.
- munificent 4y agoThat was my initial pitch too. But after months, almost no one seemed to have warmed up to it. When we changed it to "when", just about everyone seemed to like it better. Syntax design is weird. Sometimes the only way to tell if I did it right is when no one says anything. People complained when I used "if". No one did after I switched it to "when". <shrug>
- vlovich123 4y agoIt’s always bugged me when syntax selection is done by popular vote. The most frequent vocal complaints tend to be from people who just have to adjust to something new. But if you only ever show a user what they’ve already seen, you inhibit the ability to innovate / try new things. The flip side is that it also bugs me when language / library designers make weird choices that don’t seem to have a compelling reason (or at least the compelling reason for the difference doesn’t outweigh the cost of being different). No win situation ultimately except to figure out who has good language tastes and weight their feedback more, but that’s subjective and something people try to avoid, ignoring that they’ve already done this by virtue of limiting who the experts are working on it in the first place.
- Supermancho 4y ago> if you only ever show a user what they’ve already seen Familiarity and intuitive structure in a low context language is very important. A language syntax is for humans to comprehend. If a new syntax is under discussion, it's probably around new functionality (or sugar). Where is "encourages innovation" in the list of reasons to use any specific syntax? I would say, far down on the list.
- garblegarble 4y agoThank you, that's excellent insight into the thought process for the same feature at about the same time. You mention about the human-centred nature of syntax design... do you have any instinct for why one route vs another felt right to users? Do you feel like you've developed a better instinct for this over time, or is it still hard to predict what will feel natural to users?
- munificent 4y ago> do you have any instinct for why one route vs another felt right to users? That's a good question. It is something I spend a lot of time thinking about when I see how users react to a design. In this case, I don't think I have a good answer as to why "when" seemed to go down easier than "if". > Do you feel like you've developed a better instinct for this over time, or is it still hard to predict what will feel natural to users? I'd like to believe so, but "if" was my first pick, so I guess not. :) I think what our team does have now that really helps is better processes to evaluate a design, talk about it, get feedback from users, and incorporate that feedback into the design. It's all pretty informal, but I think we're iteratively able to get designs users seem to like. But it's always really hard. There are so many trade-offs and users have different preferences and expectations, so finding the right balance is always difficult. I think that's why it's so endlessly fascinating to me: you can never fully "solve" syntax design.
- dart_fan 4y agoVery excited to hear this, if true, I will pick up Dart & Flutter again after this is released.
- munificent 4y agoIt's very true. If you build the bleeding edge Dart SDK yourself, it's already turned on. It will be enabled in the next upcoming beta release.