-
Notifications
You must be signed in to change notification settings - Fork 88
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
Reconsidering spelling of if
and else
#208
Comments
i think this example isn’t confusing due to the indentation, but it obv could be written in a confusing way: match (res) {
when ({ status: 200 }) { ok(); }
when ({ status })
if (400 <= status && status < 500) { err(); }
else { noResponse(); }
} (and of course, |
Along a similar veign: match (data) {
if (1) 'one'
if (2) 'two'
else 'other'
} The second "if" is really behaving as an "else if". I don't find these issues particularly confusing, and I feel others would quickly get used to them if they do initially find them confusing, but I also see value in exploring other keywords if people want to suggest alternative ideas. |
@theScottyJam true but i don't think we should be concerned whatsoever with someone who's not using any matching, since you'd be able to do this without this proposal (your example would require curly braces on the RHS as well): do {
if (data === 1) { 'one' }
if (data === 2) { 'two' }
else { 'other' }
} |
How about |
@MaxLOh isn't it a bit verbose? |
@ljharb - That was a simplified example (and slightly incorrect example - I see I just put numeric constants in the if instead of comparing it with something). You can throw in a few Either way, it's not a big concern of mine (nor is the original issue) - anyone who understands the rule that "only the first match expression that matches will execute" shouldn't get too tripped up by any of this.
Aren't you allowed to just put expressions on the RHS side without |
@theScottyJam no, not at the moment. see #181. |
Reading all this back, I don't see a reasonable way to change the spelling of EDIT: Speaking of, I was never clear on whether |
I guess there's one way to have an alternative spelling to "if" - in #182 if we go with the idea of taking out the For example: // do this
match (x) {
({ a: 1 }) { 2 }
({ a: 2, b }) when (!isNaN(b)) { 3 }
otherwise { 4 }
}
// instead of this
match (x) {
when ({ a: 1 }) { 2 }
when ({ a: 2, b }) if (!isNaN(b)) { 3 }
else { 4 }
} Just pointing this out, I would rather not do this, but it is an option. |
There's bunch of discussion about replacing match (x) {
when ({ a: 1 }) { 2 }
when ({ a: 2, b }) and (!isNaN(b)) { 3 }
else { 4 }
} |
|
|
I'm also a bit uncomfortable with the possibility of Remove
|
I agree that it's a bit weird... But it's really hard to know if it'll actually be a problem in practice... 🤔 In case an extra data point is useful, I personally have a slight preference for the |
I feel pretty strongly that We could spell "when" differently, or "else", but there's a lot of aesthetic value from both of those having 4 letters imo :-) perhaps a 4-letter alternative for |
Can you provide detail about why you feel that way? If it's something like "consistency with other languages", then I don't understand your objection to renaming P.S. I also noticed that none of the other languages have a special catchall syntax, even the normally |
The feature is called "pattern matching". I think forcing the common case where a catch-all will be needed into using |
Also, to address |
At this juncture, the champions group is split between |
In particular, while I remain unconvinced, a compelling argument against Note that the syntactic overlap with switch worsens significantly iff we use |
How about: match (res) {
{ status: 200 } => { ok(); },
{ status } when (400 <= status && status < 500) => { err(); },
_ => { noResponse(); }
} Or res match {
{ status: 200 } => { ok(); },
{ status } when (400 <= status && status < 500) => { err(); },
_ => { noResponse(); }
} ? Also, the later one can be examined as an expression instead of a statement so it can be used in assignment: const result = res match {
{ status: 200 } => 'ok',
{ status } when (400 <= status && status < 500) => 'err',
_ => 'other'
} |
@hez2010 as has been stated many times, |
I've now been fully convinced that we need to change I'm on board with Yulia's idea of using |
This could be any word without ambiguity, right? If so, I’d suggest
|
|
I don’t think “none” fits well; it doesn’t read like a sentence, and it implies nothing will match, but that very clause does |
I think we might have swapped the word around wrong. Perhaps "anything" would be a more appropriate keyword if we want to go this more declarative route, implying that the "anything" matcher will match against whatever comes its way. Now that you point it out, I agree that "none" should technically be seen as "nothing will match this", in the pattern-matching mental model. match x {
when ({ y: 2 }) ...
anything ...
} |
anything… else? |
Not 100% sure this works from a parsing perspective but what about using match (res) {
when ({ status: 200 }) { ok(); }
when ({ status }) if (400 <= status && status < 500) { err(); }
... { noResponse(); }
} This might read better with the arrow separator: match (res) {
when ({ status: 200 }) -> { ok(); }
when ({ status }) if (400 <= status && status < 500) -> { err(); }
... -> { noResponse(); }
} |
I actually kinda like Not a big fan of |
True, I can see how it's unclear now. The idea was to communicate "other" (descriptive of the match) instead of "otherwise" (descriptive of what to do? unsure of the right term). There are likely better ways to do that. |
|
if that is the only better way, I think we must not speak the same English. that reads like a hoofed turtle to me: it's confusing, it doesn't register that the two parts are connected. |
lol true enough. it's a bikeshedding thread, ideas don't have to be good. |
Oh, I misread that as reiterating a preference for "else", not a new suggestion for "when else". I suppose the description I gave for how I perceive that still works, but I would have tempered its ... colorfulness if I'd realized it was a new suggestion. |
|
elsewhen is a real word — as is the slightly more common (historically) “anywhen.” Both are probably too obscure/archaic, though it’s interesting that both have increased in usage during the last forty years. forgive me for this but I couldn't help but imagine what the whole thing might look like with the olde h/th/wh wordshence (hitherable) {
whence ({ status: 200 }) thither ok();
whence ({ status }) where 400 <= status && status < 500 thither err();
anywhence noResponse();
} |
@bathos might as well throw match (res) {
when ({ status: 200 }) ok();
when ({ status }) if (400 <= status && status < 500) err();
lest noResponse();
} |
Was reading #259 & rereading this thread and wanted to add some additional suggestions that occurred to me, in case the champions group revisits this decision: when(...): expr; // modification of a suggestion I made above
when: expr; // new suggestion, no matcher or parens at all |
Hm, don't recall if we ever considered that. Doesn't look bad to me, personally. |
Perhaps another option would be to provide a new global matcher. e.g. something like this: when (Any): expr
// or
when (MatchAll): exp Where Edit Forgot that those custom matchers would need to be "interpolated" like this when (${Any}): expr
// or
when (${MatchAll}): exp ...which is much less desireable |
I definitely don't like the same token being used for "to match something" as "the default matcher". |
What about match (res) {
when ({ status: 200 }): ok()
when ({ status }) if (400 <= status && status < 500): err()
finally: noResponse()
} |
That implies it always runs after the matched clause, just like in try/catch. |
Oh, I misread the earlier comment - I thought the The one I didn't think looked bad was just the plain (I'm still weakly supportive of keeping |
As we discovered in #206, statements like the following have the potential to cause confusion:
Specifically, the concern is that folks learning how to use pattern matching will misconstrue the
if
on the second clause as being attached to theelse
clause, which would change the mental model of execution here.Do we have a way to investigate how prevalent this confusion would be? Perhaps using the cognitive dimensions of notation framework that Yulia and Felienne presented last year?
The text was updated successfully, but these errors were encountered: