-
Notifications
You must be signed in to change notification settings - Fork 0
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
Prefer not in
and not instanceof
#1
Comments
|
I very much dislike using TS’s choices as a reason to change the language design, even when it supports my preference. |
I think it's very important to understand how the design affect the popular tools in our ecosystem. In this specific case, Actually after noticing the ambiguity I finally realized why Kotlin (which adopt |
I agree that it's a conflict with TS, and that might motivate some to prefer keywords over syntax. I'm just saying that I'm not comfortable with "not JavaScript" forcing the design of JavaScript, even when it supports a change I want :-) |
I also strongly favor |
There is indeed a TS incompatibility here. Thanks for pointing this out - this was also raised during the TC39 meeting. I investigated the issue using Sourcegraph to look at occurrences of Out of these 13 unique occurrences, it looks like most if not all usages of I've listed some clear bug examples (out of the occurrences) below: Bug examples
I plan to follow up with the TypeScript team to discuss how to treat this incompatibility based on the data above. If possible, I'd like to keep both syntax options open, and choose one or the other based on their own merits.
|
Didn't see this ratified from a purely in-language context, i.e. without any relation to other programming languages, so here are some observations about existing syntax forms in ECMAScript. AFAIK there are either zero or few JS keywords which combine symbols and letters to convey meaning — here are the possible exceptions:
There aren't that many multi-word keywords in JavaScript, but it's nowhere near unheard of:
Note that I'm not counting unary expressions like There are loads of operations which combine symbols from a particular suite of operations with assignment, getting you a "composite" meaning you can maybe guess the first time you come across
But in general we don't seem to combine symbols in a meaningful manner as much as words.
I want to give special attention to the forms However, I also have to acknowledge that basically all of the examples of combining words happen in statements, not as operators within expressions. We do use words in expressions a lot — Still, I think that's worthwhile ground to break. This is totally an IMO statement, but — IMO 😸 — there just isn't much approachably predictable rhyme or reason to combining symbols with words, or symbols with other symbols, really. See the list above: it's a big mixed bag. The other inherent advantage to using words for keywords is that they force you, very obviously, to put spaces between the operator and operand. Here's some real shenanigans: x!instanceof Y /* The horror! Spacing on the right, none on the left! */ I don't know for sure how ECMAScript parsing works so I'm not sure if this is legal or not, but it certainly looks like it could be... and it just isn't nice. No one wants to figure out what that means. Sure it's on the developer for styling to their own preferences, but (Also a quick case on conciseness: |
The (currently also in stage 1) pattern matching proposal has a It will allow |
Correct me if I'm wrong here
So you're saying that this is ambiguous for you try {...} catch (error) {
if (error !instanceof TypeError) {...}
throw error
} But this is completely ok, right? async function init() {
if (!navigator.gpu) {
throw Error("WebGPU not supported.");
}
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
throw Error("Couldn't request WebGPU adapter.");
}
const device = await adapter.requestDevice();
//...
} Understand me right here, the argument would be valid if we would live in the world where JS has You may say that those are different from specification, but from human vision are not.
Both cases use
This is unarguable though |
I assume by "both cases" you mean the two examples you gave. It's true that we use I think it's subjective whether you conceptualize Personally (speaking for myself not ljharb), I wouldn't say that
I believe (correct me if I'm wrong here LOL) that the main point of "potential ambiguity", though, is to do with TypeScript syntax. Keep in mind in JavaScript we always use Prefix (in JavaScript and TypeScript): The operator in this proposal (if not changed to However, there is nothing saying there has to be whitespace between the left-hand expression and the This is trouble, because in TypeScript, that already has a different meaning. After all, @hax summarized the conflict well: #1 (comment) |
Just a small note: |
@HolgerJeromin I don't think the argument is wrong per se, it's just — not related to the reading I was pointing to. Like I said, "there," it's "always" combined with In practice it is likely that most developers experience
Perhaps you also run into These are all likely to inform how you see I understand how it reads like I meant that, if you see "if-not" as its own thing, then you literally never use Of course, you can basically summarize your understanding of I describe two ways you might relate your understanding of TL;DR yes you can encounter |
Rather than making it more syntax soupy, and combining the
!
symbol with a word, "not in" and "not instanceof" read more clearly to me and are much less potentially ambiguous.This also meshes nicely with pattern matching, in which
not
would be part of the pattern syntax.The text was updated successfully, but these errors were encountered: