Re: Re: Existential Operator / Null Propagation Operator
>From a technical point of view, using ![ instead of ?.[ may work only if you >forbid a line terminator before the ! I tried this on [TS Playground](http://www.typescriptlang.org/play/#src=var%20a%20%3D%20%7B%7D%3B%0D%0A%0D%0Aa!%5B3%5D%3B%0D%0Aa%0D%0A!%5B3%5D%3B) and it interestingly changes behavior when there is a line break before `!`. ```js var a = {}; a![3]; // works as a[3] a ![3]; // preserves original behavior ``` >but many languages (e.g. Swift, Kotlin, and I think TypeScript 2.0) use it in >the inverse direction: non-null assertion for nullable types. Right. :/ ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Existential Operator / Null Propagation Operator
On Thu, Oct 13, 2016, 12:07 Claude Pachewrote: Le 13 oct. 2016 à 17:14, Kagami Rosylight a écrit : >IIRC the proposed syntax for computed properties was x?.[y], Yes you’re right, sorry :/ IMO it still seems the syntax problem is the main reason why this proposal has stalled. If not, what is the problem here? The issue with `?.[` is that it is considered as not pretty by some people. A syntax that is at the same time pretty, technically working, and not confusing is difficult to find. I agree with both points here. It's not very pretty, and it's also inconsistent with the rest of the language. I was just clarifying what I believed to be the primary contender, independent of bias. Concerning your suggestion of using `!`: From a technical point of view, using `![` instead of `?.[` may work only if you forbid a line terminator before the `!`, because the following program is valid as of today (with implied semicolons): ```js foo ![42] ``` I want to like the idea, but many languages (e.g. Swift, Kotlin, and I think TypeScript 2.0) use it in the inverse direction: non-null assertion for nullable types. I'm not sure I like the syntax in either form (it has at least the ability to be non-ambiguous). I’m curious why this proposal is not even listed in stage 0 proposal list. Because no representative of TC39 has volunteered to champion it. —Claude ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Existential Operator / Null Propagation Operator
> Le 13 oct. 2016 à 19:20, Bob Myersa écrit : > > Why is this needed? Why are people trying to get the property of an object > which is null? Why is the object null in the first place? This is not about trying to get something from null, but about taking different paths according to when a reference is null or not without needing to assign to temporary variables, writing complete `if` structures, and/or repeating oneself. > (...) > > Just as an example, consider the following idiom for null propagation: > > ``` > a ? a.b ? a.b.c : undefined : undefined > ``` > > We can leverage this pattern by allowing the `:` in the ternary operator to > be omitted (defaulting to undefined), allowing us to write: > > ``` > a ? a.b ? a.b.c > ``` > > Whether you love it or hate it, at least this solves more problems that just > null propagation. I'm not seriously suggesting this. I'm just saying we need > to be more creative in brainstorming possible solutions to the problem. You can already write`a && a.b && a.b.c` in JS... but you still have to repeat `a` thrice and`b` twice, which is an issue if `a` and `b` are complex or lengthy expressions. Sometimes I am tempted to write something in the lines of `(_ = a.b) && _.c` in order to avoid the issue, but it is less readable. Here is a more complex although somewhat contrived example: If an element named "foo" is inside a section, open the latter in order to reveal the former: ```js document.querySelector("input[name=foo]")?.closest(".details")?.open = true ``` (The short-circuiting mechanism, which is an important part of the semantics in my proposal, ensures that the assignment is performed only when the expressions just before the `?`s are not null/undefined.) —Claude ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Existential Operator / Null Propagation Operator
I think the point is that people would like to write something like this: if (person?.address?.zipcode) instead of this: if (person && person.address && person.address.zipcode) That appeals to me. On Thu, Oct 13, 2016 at 12:20 PM, Bob Myerswrote: > Why is this needed? Why are people trying to get the property of an object > which is null? Why is the object null in the first place? This can probably > be considered poor program design. It's sort of like trying to dereference > a null pointer. In addition, parameter defaults and defaults in > destructuring may make this somewhat less of an issue. > > Note that TS2 is explicitly moving away from permitting null to be > assigned to something which is alleged to be an object. (Although TS2 has > "stolen" the `!` operator, it is merely a type assertion--a narrowing from > `object | null` to `object` as I understand it. It is not a run-time check.) > > But let's say we nevertheless think this is an important feature. It has > been discussed at great length here. No proposal has ever had the > inevitability, generality, or intuitiveness that would allow it to gain > traction. All the proposals are essentially little syntactic hacks. > > Can we find some more general extension to JS syntax that solves or > mitigates this problem as well as others? Kills two birds with one stone? > One that seems like a natural extension to current syntax, instead of an > extra magic character we stick somewhere to solve one specific problem? > > Just as an example, consider the following idiom for null propagation: > > ``` > a ? a.b ? a.b.c : undefined : undefined > ``` > > We can leverage this pattern by allowing the `:` in the ternary operator > to be omitted (defaulting to undefined), allowing us to write: > > ``` > a ? a.b ? a.b.c > ``` > > Whether you love it or hate it, at least this solves more problems that > just null propagation. I'm not seriously suggesting this. I'm just saying > we need to be more creative in brainstorming possible solutions to the > problem. > > -- > Bob > > > On Thu, Oct 13, 2016 at 9:37 PM, Claude Pache > wrote: > >> >> Le 13 oct. 2016 à 17:14, Kagami Rosylight a >> écrit : >> >> >> >IIRC the proposed syntax for computed properties was x?.[y], >> >> Yes you’re right, sorry :/ >> >> IMO it still seems the syntax problem is the main reason why this >> proposal has stalled. If not, what is the problem here? >> >> >> The issue with `?.[` is that it is considered as not pretty by some >> people. A syntax that is at the same time pretty, technically working, and >> not confusing is difficult to find. >> >> Concerning your suggestion of using `!`: From a technical point of view, >> using `![` instead of `?.[` may work only if you forbid a line terminator >> before the `!`, because the following program is valid as of today (with >> implied semicolons): >> >> ```js >> foo >> ![42] >> ``` >> >> I’m curious why this proposal is not even listed in stage 0 proposal list. >> >> >> Because no representative of TC39 has volunteered to champion it. >> >> —Claude >> >> >> ___ >> es-discuss mailing list >> es-discuss@mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> >> > > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -- R. Mark Volkmann Object Computing, Inc. ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: Existential Operator / Null Propagation Operator
>Why is this needed? Why are people trying to get the property of an object >which is null? I will appreciate null propagation when a function receives an “option bag” ```js function someFunction(options) { if(options?.foo) { doSomething(); }; } someFunction(); someFunction({ foo: true }); ``` ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Existential Operator / Null Propagation Operator
Why is this needed? Why are people trying to get the property of an object which is null? Why is the object null in the first place? This can probably be considered poor program design. It's sort of like trying to dereference a null pointer. In addition, parameter defaults and defaults in destructuring may make this somewhat less of an issue. Note that TS2 is explicitly moving away from permitting null to be assigned to something which is alleged to be an object. (Although TS2 has "stolen" the `!` operator, it is merely a type assertion--a narrowing from `object | null` to `object` as I understand it. It is not a run-time check.) But let's say we nevertheless think this is an important feature. It has been discussed at great length here. No proposal has ever had the inevitability, generality, or intuitiveness that would allow it to gain traction. All the proposals are essentially little syntactic hacks. Can we find some more general extension to JS syntax that solves or mitigates this problem as well as others? Kills two birds with one stone? One that seems like a natural extension to current syntax, instead of an extra magic character we stick somewhere to solve one specific problem? Just as an example, consider the following idiom for null propagation: ``` a ? a.b ? a.b.c : undefined : undefined ``` We can leverage this pattern by allowing the `:` in the ternary operator to be omitted (defaulting to undefined), allowing us to write: ``` a ? a.b ? a.b.c ``` Whether you love it or hate it, at least this solves more problems that just null propagation. I'm not seriously suggesting this. I'm just saying we need to be more creative in brainstorming possible solutions to the problem. -- Bob On Thu, Oct 13, 2016 at 9:37 PM, Claude Pachewrote: > > Le 13 oct. 2016 à 17:14, Kagami Rosylight a écrit > : > > > >IIRC the proposed syntax for computed properties was x?.[y], > > Yes you’re right, sorry :/ > > IMO it still seems the syntax problem is the main reason why this proposal > has stalled. If not, what is the problem here? > > > The issue with `?.[` is that it is considered as not pretty by some > people. A syntax that is at the same time pretty, technically working, and > not confusing is difficult to find. > > Concerning your suggestion of using `!`: From a technical point of view, > using `![` instead of `?.[` may work only if you forbid a line terminator > before the `!`, because the following program is valid as of today (with > implied semicolons): > > ```js > foo > ![42] > ``` > > I’m curious why this proposal is not even listed in stage 0 proposal list. > > > Because no representative of TC39 has volunteered to champion it. > > —Claude > > > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Existential Operator / Null Propagation Operator
> Le 13 oct. 2016 à 17:32, Isiah Meadowsa écrit : > > It may be a good idea to create a pull request for it if it isn't listed yet > I've already tried some time ago: https://github.com/tc39/ecma262/pull/340 —Claude___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Existential Operator / Null Propagation Operator
> Le 13 oct. 2016 à 17:14, Kagami Rosylighta écrit : > > > >IIRC the proposed syntax for computed properties was x?.[y], > > Yes you’re right, sorry :/ > > IMO it still seems the syntax problem is the main reason why this proposal > has stalled. If not, what is the problem here? The issue with `?.[` is that it is considered as not pretty by some people. A syntax that is at the same time pretty, technically working, and not confusing is difficult to find. Concerning your suggestion of using `!`: From a technical point of view, using `![` instead of `?.[` may work only if you forbid a line terminator before the `!`, because the following program is valid as of today (with implied semicolons): ```js foo ![42] ``` > I’m curious why this proposal is not even listed in stage 0 proposal list. Because no representative of TC39 has volunteered to champion it. —Claude ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: Existential Operator / Null Propagation Operator
It may be a good idea to create a pull request for it if it isn't listed yet (search "null propagation JavaScript"). I know there's a proposal written out (I've seen it), I just don't recall the exact URL offhand nor if there's a champion or not, but I thought it did. It could be one of those looking for a new champion (that's why the bind operator proposal has also stagnated). On Thu, Oct 13, 2016, 11:14 Kagami Rosylightwrote: > > > >IIRC the proposed syntax for computed properties was x?.[y], > > > > Yes you’re right, sorry :/ > > > > IMO it still seems the syntax problem is the main reason why this proposal > has stalled. If not, what is the problem here? I’m curious why this > proposal is not even listed in stage 0 proposal list. > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: Existential Operator / Null Propagation Operator
>IIRC the proposed syntax for computed properties was x?.[y], Yes you’re right, sorry :/ IMO it still seems the syntax problem is the main reason why this proposal has stalled. If not, what is the problem here? I’m curious why this proposal is not even listed in stage 0 proposal list. ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: Existential Operator / Null Propagation Operator
IIRC the proposed syntax for computed properties was `x?.[y]`, to avoid the ambiguity. On Thu, Oct 13, 2016, 10:24 Kagami Rosylightwrote: > > > >The token ?. works fine > > > > I think more than half of this thread is about syntactic ambiguity, > regardless of whether the ambiguity is real or not. For example, from [an > earlier post of this thread]( > https://esdiscuss.org/topic/existential-operator-null-propagation-operator#content-44 > ): > > > > >But what should be done with cases like obj?[1]?[2]:[3]. > > > > A formatter may help this and make it `obj?[1] ? [2] : [3]` or `obj ? > [1]?[2] : [3]` depending on operator precedence, but shouldn’t it be more > clear? `obj![1]?[2]:[3]` will not be confused with ternary operator. > > > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: Existential Operator / Null Propagation Operator
>The token ?. works fine I think more than half of this thread is about syntactic ambiguity, regardless of whether the ambiguity is real or not. For example, from [an earlier post of this thread](https://esdiscuss.org/topic/existential-operator-null-propagation-operator#content-44): >But what should be done with cases like obj?[1]?[2]:[3]. A formatter may help this and make it `obj?[1] ? [2] : [3]` or `obj ? [1]?[2] : [3]` depending on operator precedence, but shouldn’t it be more clear? `obj![1]?[2]:[3]` will not be confused with ternary operator. ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Existential Operator / Null Propagation Operator
> Le 13 oct. 2016 à 14:37, Kagami Rosylighta écrit : > > > Or `!.`, which unfortunately is now being used by TypeScript? What is exactly the issue you're trying to solve? The token `?.` works fine (technically with a simple lookahead for excluding digit after it). —Claude ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: Existential Operator / Null Propagation Operator
TypeScript can change if it has to, and it's done so before (ES modules are a good example of this). They try their best to be a strict superset of ECMAScript, and this even goes as far as making type errors early warnings, not early errors, by default (the latter would technically be a violation of section 16, paragraph 3). On Thu, Oct 13, 2016, 08:37 Kagami Rosylightwrote: > > > Or `!.`, which unfortunately is now being used by TypeScript? > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: Existential Operator / Null Propagation Operator
Or `!.`, which unfortunately is now being used by TypeScript? ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss