Open chicoxyzzy opened 7 years ago
As spec'd (with the caveat that it wouldn't match the grammar at all unless something was changed), a?.#b
would differ from a.#b
only in that if a
is undefined
or null
then no error is thrown in the former case. I expect that would cause more confusion than it would prevent, so yeah, I think this should be a Syntax Error.
(Also, that would later allow us to redefine a?.#b
to be undefined
instead of throwing when a
lacks a #b
field, which would be kind of neat. Although maybe also too confusing to be worthwhile.)
@bakkot makes a reasonable argument that this feature is relatively complicated and confusing, and it's harder to see the use case than ordinary null propagation. However, syntactically, this works out just fine: a?.#b
is a clean composition of the two features. And that's not the case with some alternatives, such as a.?b
, for null propagation syntax. Let's keep an eye on the interaction between these features--even if we have semantic reasons for not wanting to add the combination now, it's nice that the current proposals are compatible in this way.
Btw, what was the rational regarding the use of the #
character? I find the .#
sequence a bit hard to type, since it requires a combo key (I'm on an azerty keyboard, one has to press Alt Gr+3). If possible, I'd prefer something like foo.*bar
or foo.:bar
. My two cents.
The current proposal remains .#
.
Sorry, I (personally) wasn't thinking about non-US keyboards with this proposal; in American keyboards, # doesn't require pressing Alt Gr.
However, given that, on a French keyboard, {
and }
are Alt Gr + number bar key combinations, it seems like the ship has already sailed on JS requiring these keys.
Leaving keyboard layouts aside, *
already has a couple meanings (yield*
and multiplication), and :
also has a couple (property definition, labels, and in many compile-to-JS languages, type definitions), so overloading another seems risky in terms of promoting understanding. They also cause issues when taking the short form into account.
For now, I'd like to stick with .#
.
Null Propagation was promoted to stage 1 on January meeting. How should this work with private fields? AFAIU private properties are statically resoled so I suppose that something like
a?.#b
should be an early Syntax Error?cc @gisenberg