Replies: 2 comments 2 replies
-
Regarding 1, I agree. I'd rather suggest Regarding 2: Indeed, that's how it is right now. We still need to add support for arbitrary guards. I could imagine something like the following syntax: mypat := /\s/
switch 'an example subject'
.test mypat
console.log 'contains whitespace'
// or
&.test mypat
console.log 'contains whitespace' Some more related discussion is in #480. Let us know if you have further suggestions. The meaning of A possible workaround for now: mypat = '\\s'
switch 'an example subject'
///${mypat}///
console.log 'contains whitespace' ...except that |
Beta Was this translation helpful? Give feedback.
-
On (1), I mean sure. That would help. As would an explicit comment "note without the On (2), since just a bare word as a case is a parse error, is there a reason not to check if it's (typed as?) a pattern and if so, do pattern matching there? I guess one reason is why favor pattern matching over other forms of case selectors -- although the The difficulty with your proposed Finally, /// patterns do work in some cases in switch statements, see #831, which is exactly what I am currently using. However, even though all of
work fine, the case selector |
Beta Was this translation helpful? Give feedback.
-
PATTERN.test(SUBJECT)
, a pattern in a switch statement succeeds if SUBJECT merely contains a match to PATTERN, i.e., all of SUBJECT need not match. I think the very first example under Pattern Matching on the Reference page is misleading, because it includesBut (A) down that branch all you know is that the SUBJECT contains a whitespace character (so you might consider changing the code for that case to say
console.log "contains whitespace"
), not that it "is" whitespace, and (B) unless there is some way of fetching the matching substring in this construct -- and I don't think there is -- then the pattern clause/\s+/
is precisely equivalent to/\s/
, so why would you ever use the more complicated/\s+/
? I recommend changing to/\s/
in the example.there doesn't seem to be a way to re-use a pattern in multiple switches; just
mypat
in the condition is a parse error and^mypat
tests whether 'an example subject' is === to mypat, which is not helpful. Or am I missing something? Thanks for any thoughts/guidance.Beta Was this translation helpful? Give feedback.
All reactions