-
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
Bikeshed issue: combinator spelling (|
, &
)
#179
Comments
I
, &
)|
, &
)
Intuitively I thought of |
It's certainly possible, since "pattern space" is new, so we can do basically whatever we want. The overlap with TypeScript types is actually good imo, because it has the exact same semantics. Using I'll add the suggestion to the list! |
A third option: Python-style |
words like "or" and "and" is also Ruby-style; that's not a bad suggestion either! |
@ljharb C# also uses var number = 3;
if (number is 3 or 6) {
Console.WriteLine("number is 3 or 6");
}
var result = number switch {
3 or 6 => "number is 3 or 6",
_ => "number is neither 3 nor 6"
}; I'd vote first for |
I'm fairly opposed to |
both |
Echoing the above: I like |
I would find “or” and “and” to be a big cognitive relief — it seems too easy to misread these as expressions if they repurpose tokens that are normally operators. |
I'm undecided as to which I prefer out of |
I feel like someone's gotta ask the question... Is it worth considering unicode characters? E.g. The mathematical symbols for union and intersection, |
LOL, you should use Logic Or “∨” (U+2228) and Logic And "∧" (U+2227) instead of union and intersection. And future JS devs will have a cheat sheet file on their desktop so they don't need to copy-paste that from MDN again and again. |
Yeah I know the DX is far from ideal. It just might be worth explicitly stating that unicode characters are out of scope if that's the case. 😀 |
@treybrisbane explicitly, they are always out of scope. |
|
As for the con of |
Charging
The tradeoff to these problems is quite minimal: one less keystroke, and a postulated convenience for TypeScript programmers.
I'm sorry, what. |
@Alhadis there's a whole eslint rule, |
Building on that, if we can do something like +1 for using |
|
, &
)|
, &
)
@silicakes thanks for that comment, that's a great point - regexes are probably the closest intuition that we can hope to draw on here. I've updated the OP to add that to the "pros" column. |
This comment has been minimized.
This comment has been minimized.
Note: it does not. Arbitrary operations, including bitwise ops but also every other operator, are already impossible in the general pattern syntax. (You can't write If you use the pin operator, you break out into general expression syntax, and you can use any operator you want, including the bitwise opts. (You can write |
My bad. I overlooked the part on leaf patterns, so naïvely assumed "pattern" meant "any valid ECMAScript expression". The other points still stand though. |
Sure, just wanted to make that particular correction, because if it were correct it would be a significant downside on its own. |
The champion group largely prefers (but not unanimously) using We'll run this by plenary during our next presentation. |
This list may not complete, but most of the language here use Another disadvantage is if |
@t7yang how could it possibly break existing code? they'd only be keywords inside a pattern context, which no existing code is. |
@ljharb OK, my mistake, we should use |
@t7yang the current proposal uses |
@ljharb Just curious, why on earth was
Given that regular expressions are often referred to as "patterns", |
The RegExp uses (for ^ but also | or anything else) seem pretty different to me. RegExp literals usually look very distinctive, not super confusable w/ other constructs (to me anyway; maybe not to everyone. certainly not to lexers :). Most RegExp "syntax character" + operator homop - homoglyph(?) - pairs have unrelated meanings. "Brackets surround things, creating groups" is pretty much as far as it goes, right? Given the RegExp grammar is a narrow DSL for describing other regular(-ish) grammars, this seems pretty natural. Superficial alignments might even be a downside if they could imply a stronger analogy than really exists. (Not for or against ^. These are just my thoughts on how RegExp syntax may not be a great place to seek consistency or precedent.) |
@bathos That wasn't the point I was making. I brought up Personally, I'd use a bare backslash to suppress the usual interpretation of a pattern: [[1]]: Or whatever the correct term is. @Jack-Works, send some of [that](https://github.com//issues/179#issuecomment-824699435) juicy mathematical enlightenment this way. 👍 EDIT: I'm leaving that abortion of markdown parsing intact, because holy hell, what the actual fuck just happened? |
@Alhadis Your positions make sense to me; my comment may have seemed more specific/counterargumenty than I intended. Other folks had mentioned RegExp syntax previously and seeing it again tripped a general “should RegExp really matter here?” question that was stewing somewhere in my head. While I’d answer that “probably not much,” it may still be just as true that "^" isn’t a great choice, and you have listed other reasons. |
I think one thing to consider is how long chains of pattern combinators would likely be formatted. For example in in typescript union types and in Rust pattern matching you have things like this respectively: type Value =
| Foo
| Bar
| Baz
| FooBar
| FooBaz
| FooFoo
| BarBaz
| BarFoo
| BarBar; match value {
match foo {
| Foo::Bar(value)
| Foo::Baz(_ignore_this, value)
| Foo::Quux(_ignore_this, _and_this, value) => {
println!("value = {}", value);
Whatever::One
}
Foo::Spam(value) | Foo::Eggs(_, value) => Whatever::Two(value),
}
} In both cases the I think this is desirable for readability, but this isn't really an option for match (status) {
when (
| 200
| 201
| 202
| 203
) {
// ...
}
} match (status) {
when (
or 200
or 201
or 202
or 203
) {
// ...
}
} |
You wouldn’t be able to start with a combinator regardless, just like how && and || work. |
There is no reason why it couldn't. Patterns are all open design space because they don't exist yet. The spec could allow a leading character. Whether it should can be open to debate. |
We could certainly discuss it, but i don’t like it myself, and i suspect many delegates would push back on it for a number of reasons. Either way, it’s a weak argument in favor of | and & as combinators. |
Allowing a leading "|" would make things awkward if we ever decide to add an "&" combinator, or any other kind of combinator. I don't think it makes sense to allow an operator-like symbol in a leading or trailing position of something. |
The current proposal includes both | and & (or “and” and “or”). There are no plans to add one without the other. |
At this juncture, the champions are split between Our conclusion is that the feature is dramatically more important than the spelling. We plan to present this to committee with both options available, and fall back on the word spellings if we get pushback on the symbol spellings. I'll be updating the README to reflect this conclusion. |
For the pattern combinators, the champion group chose spelling that we believe will be widely intuitive to the most developers. TypeScript type notation, as well as pattern matching/case selection in a number of languages, use the pipe (
|
) and ampersand (&
) for "or" and "and" semantics, respectively.There is a potential confusion here with JS' bitwise OR (
|
) and bitwise AND (&
), but our belief/hope is that users will understand that "pattern mode" is distinctly different (many things that work fine in other parts of the language will be syntax errors where patterns are expected), and will immediately learn that3 | 4
means "3 or 4", even if they expect it will mean "7" (which, since most users avoid and do not understand bitwise operators, we expect most users will not expect).Do you have any alternative suggestions? Please provide compelling arguments for them if possible, and I'll edit the OP to compile a list of the viable ones!
Options:
|
and&
/(foo|bar)/
)||
and&&
pros: intuitive to JS userscons: conceptual overlap with value selection opsor
andand
The text was updated successfully, but these errors were encountered: