-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Null-aware subscript operator #28389
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
Comments
Syntactically, what about That fits with darts And what about other overloaded operators? |
The null aware getter / call operator is I believe extension to indexing via If operators were allowed to be used in a call-like syntax, e.g. I think we would need the |
Dang. Always get them confused. Thought I saw '.?' in a discussion about this and went off of that. Hmm, well, In that case Of course, that might be a crappy error in terms of diagnosability from someone who doesn't know there's a So not necessarily ambiguous, but not quite backwards compatible. At least '?+' isn't ambiguous. But unary minus is a great point. +1 for for adding call-like syntax, its a feature that enables the other feature. But unary minus does make that one weird too. |
That's a good point. We could tokenize getStuff() => condition ?[foo] : [bar]; If they don't put a space there, it will get tokenized wrong. That might be tolerable, though. dartfmt always spaces that out. |
An alternative is to have a method that does the same as the return nullableMap?.lookup('property')?.lookup('subproperty') |
Adding a That's just a sign of bad design. We should make the preferred one work all the time. The only question is which syntax to use for it. Introducing new and different syntax would just be the language designers admitting defeat. We have the problems that
These are different - the former is an invocation, the latter is a reference-by-name (no arguments). Since operator syntax depends on operands for parsing, we'll probably need something different for tear-offs. There are lots of options. The biggest problem with just adding a (Tear-offs are likely easier, but there are also more options: |
One problem with using punctuation for adding null-aware variants of all the operators, which is also a problem with providing punctuation-based syntax for tearing off operators, is that you eventually turn the language into impenetrable line noise, like perl. The current syntax is mostly intuitive. The syntax for constructor tear-offs is less so (I believe I heard that syntax was going to be removed?). I would be very wary of adding more features if those features were not intuitive.
|
Yes, generalized tear-offs are gone. I think we're hoping to add in support for constructor tear-offs that matches the method tear-off syntax: var tearOff = SomeClass.someNamedConstructor;
var another = SomeClass.new; // For the unnamed constructor Personally, I think that's more intuitive than the weird |
The simplest tear off syntax faces this (syntactic but not semantic) ambiguity: if But it also has issues with tearing off unary operators. Granted, tearing off unary operators into a noarg closure doesn't seem super crazy important...but there are a lot of them ( From that perspective, I'd be inclined to say that Unary minus is treated as I think this is the best I can think of:
seems good so far, but this is where it gets fishy.
Definitely getting pretty perly. That second to last one makes me gag. Luckily I don't think its necessary. Also, if unary minus is Could be keywordized:
In both cases, Maybe its better just to catch
I don't really like I'd vote for in the style of Seeing all the other syntactic complexity that comes up, I'm starting to lean to the simplicity of having an extra method name.
It is much, much much easier to understand this code without looking at a manual. And at 11 characters minimum, |
I wouldn't use For The problem with class C {
get operator => 15;
}
main() {
var c = new C();
print(c.operator+(21+16));
} so taking that notation for something else is a breaking change. Adjacent identifiers are not used, so an alternative, as you mention, is |
It is worth noting that
Preeetty sure this is And I thought that unary minus worked like
but I was wrong. Looks like its just That makes my Once again, this makes me think the method names thing best: handles all our cases, in a way that's easy to understand without docs, in a way that doesn't look like perl, that disambiguates unary ops, and works for both tearoffs and null awareness intuitively. Hard to beat that I think. |
I think it's really unlikely we've overload |
I would have thought that would be all the more reason to allow overriding Of course, not for immutable objects like |
Yeah, I don't think it should mutate. x = x + 1; |
This ranged fairly far afield. Do we have anything actionable here on the original topic? Do we (in the short to medium term) want to push on a null aware subscript? |
I don't personally feel it's a burning issue right now. I'd rather focus on most structural language changes (strong mode, etc.) and do small-scale syntax additions later. |
Just posted #33903 and then I saw this. Hope we can get something going. |
I think we should consider |
Would it be more consistent if we allowed people to access properties of maps like they do in JS?
|
@lukepighetti That is not a Map in JS, that is an Object: // ES6 JavaScript
var object = {"foo": "bar"};
object.foo; // OK
var map = new Map();
map.set("foo", "bar");
map.foo; // Error
map.get("foo"); // OK Dart does not have true anonymous objects. You could simulate one using |
I'm not privy to the details on how these languages work, I just figured that if you could build |
.. it is quite different from what you are asking. |
Oh, sorry, I figured the and I believe
and like I said, I'm not privy on the language details, so I can't say why one is easier than the other |
Dart maps are not like JavaScript objects. They have methods and and their keys are not necessarily strings, so The notation It could be argued that the syntax should really be (We could also add |
I suspect that this is a terrible idea, but out of curiosity (and maybe for the sake of completeness), what about adding an implementation of |
@jamesderlin The point of not allowing method invocations on A "contagious null" would be one that allows every operation, and always returns null (that's how NaN works for Dart has eager errors on We can fix the |
I dunno, I kind of like it as a solution. |
Same. I expected x?[y]. No "." because it's a map, not a member variable. But no pressure from me. I don't understand the specifics easier and will take what I can get 😁 |
We are adding a null aware subscript operator as part of the upcoming NNBD release. See additional discussion here. |
For example map access operator[]. It would be nice to be able to write something like:
but there is currently no proper syntax, so one needs to either do this:
or slightly shorter but hard to read:
The text was updated successfully, but these errors were encountered: