-
Notifications
You must be signed in to change notification settings - Fork 90
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
Why when
and not |
?
#143
Comments
I have seen #140, and it doesn't make sense to me, because match clause isn't really destructuring, so prefixing it with |
I still object to using case; i insist on adding a new keyword, since it’s a new concept. Using a pipe for the conditions might work, but it seems pretty opaque - code is read much more often than it’s written, so I’d prefer to see us optimize for reading and understanding, not for writing. |
I'd rather type |
I don't see how using a pipe is opaque, it's pretty expressive and natural, this is JavaScript, not Basic. Take spread operator as an example, it'll become second nature in a couple of days of using it. |
The pipe is already used for logical or, binary or, maybe the pipeline operator, I personally feel there's enough pipes already. |
@topaxi pipeline operator uses |
|
@ljharb fair point, but I think most of users know about TypeScript/Flow unions, where it is used as OR, bitwise OR operator also should be intuitive that it is |
I can understand if there's ambiguity in parsing |
@goodmind on the contrary, i'm pretty certain most JS users have never even seen a typed language. |
@ljharb what about bitwise OR? I mean it's still OR so should be familiar enough |
most javascript-problems require rewriting your code/api so frequently that trying to lock things down with type annotation is generally anti-productive |
the familiarity is that it coerces two operands to a 32 bit integer and produces a new one. How is that similar to your suggested usage? |
@ljharb familiarity in name, OR, it's basically |
People know semantics more than names. |
@kaizhu256 this isn't about types |
How do you plan to parse these? The former is 2 cases written inline, the latter is a single case with a bitwise case (props) {
| {foo} -> foo | {bar} -> bar
}
case (props) {
| {foo} -> foo | {foo}
} How would you differentiate a case |
@dantman is parser recoverable? I think lookahead seems most plausible, but if it is too inefficient then newline is fine for me, so no inline cases |
Symbol-heavy syntax makes things less accessible for new folks. For example, everyone gets what an if/else statement does, but ternary syntax can be really intimidating. This feature's API is already somewhat complex. Power user ergonomics and parity with type notation + other languages aren't without merit -- I dig those points frealz -- but they are somewhat esoteric premises, and IMO it's not worth the cost to inclusiveness, especially since I strongly favor Thanks! Edit: *Oh boy, no pun intended |
I think @dantman nailed the key issue with using the pipe symbol for pattern matching. Parser efficiency and even just how the parser would differentiate the case pipe from bitwise pipe. If a symbol was desired, it might be worth trying to use something more like the pipeline operator: |> Of course, if someone then tried to use a pipeline within a matching clause, you would have similar problems. So perhaps: ||> Interestingly, this is ligatured with Fira Code (both are, actually): |
|
what about |
#149 also could be an idea... const fn = (vector) ->
| ({ x, y, z }) => x + y + z;
| () => new Error('vector cannot be empty');
fn({ x: 2, y: 2, z: 2 }); // 6
fn(); // Error('vector cannot be empty') |
Instead of defining a single Sorry, when I see
Its like ifs but longer words, too much noise.. I would prefer something simpler, and easier to read and understand
|
@webdeb this issue is about the pros and cons of using |
Is there any reason why a symbol or keyword is necessary at all? For example, Rust doesn't use anything and it's been fairly easy to read. As a reader, it's pretty obvious that the pattern is on the left of the Rust uses |
I think there really needs to be some kind of keyword or symbol in order for the parsing to be efficient. Parser efficiency has been a concern in other proposals as well, as js is interpreted/compiled on the fly, rather than ahead of time like Rust. This same parser efficiency concern is also part of the reason why we cannot necessarily just rely on local context to change the meaning of existing symbols, like |. Contextual meaning often requires more complex and slower parsing logic, so overloading existing operators may not be a viable solution. |
Outside of how the syntax feels ergonomically, should there be some consideration over the paradigm that this syntax is aiming at? Examples of the |
TL;DR Omit Like @IceSentry , I ask why Two separate issues:
Now for the semantics of (2): The pattern defines a type (with bindings), and a type defines a set of allowed structures. So, it is natural to regard multiple cases as a union type, which makes it obvious why So in the case when the union type has only a single item in the union, the preceding |
Terseness is not a virtue; clarity and understandability is. Code is written far less often than it is read, and thus optimizing for writability is an antipattern. |
That's why we prefer |
@ljharb +1 on that... Terseness for the sake of terseness is not a good idea. But it's IMO very doubtful that bloating your code with dozens of useless I too think that something shorter would be nicer to read. That said, I'd like to see pattern matching in JS so much that I wouldnt mind any syntax 😊 |
To elaborate upon @PinkaminaDianePie's comment, I think there's a faulty implication in some comments here that verbosity inherently increases readability, which is absolutely not a universal truth. How you define it on a case-by-case basis is relatively subjective, but I think that the readability for someone who's already aware of the syntax will be better with the terser syntax. The verbose syntax is optimising for readability specifically for the unfamiliar, which is in my view questionable at best. |
Most languages don't have a token in front of the cases at all (and sometimes |
Yes, there are use cases to match against an expression and also a pattern, which are two patterns that must be distinguished. There's also the scenario of a guard without a pattern, and also a default/else/fallback case - so there will have to be some kind of token. How it's spelled isn't particularly important in stage 1 - that's a stage 2 concern. |
From most languages I have seen, the pattern matching takes an optional guard. This allows for the bound variables to be used in the guard's expression, to further narrow the match beyond what is possible to express with type syntax alone. For example:
|
@ljharb Aesthetics is certainly important, which is why I specifically mentioned it. |
Right but there should also be support for an if without a pattern - ie, avoiding unconditional match clutter. |
I was going to mention this, as well as the catch-all case having no pattern and no guard. Then I thought maybe it was intentional in other functional languages to have to write |
The proposal has been updated in #174. It uses Please file a new issue if, taking into account the updated proposal, you have any concerns. |
This seems fewer characters to type and avoids adding new keywords to the language
This also works naturally with
or
patternSyntax inspired by ReasonML and OCaml
The text was updated successfully, but these errors were encountered: