-
Notifications
You must be signed in to change notification settings - Fork 213
Implementations allowing Ext(c)?.foo()
.
#677
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
We decided that The case for extension methods is even stronger: It has been one of our standard examples for extension methods that it can be So In general, the nnbd feature spec says that 'it is a warning to use a null aware operator (?., ?.., ??, ??=, or ...?) on a non-nullable receiver'. We could extend this to say that it is then also a warning (or similar) to use This means that we currently support avoiding to execute an For consistency, we should then also allow This is consistent with the behavior of the front end as well as the analyzer today: extension E on A? {
foo() { print("foo!"); }
}
class A {}
main() {
A? a = null;
a?.foo(); // OK.
} With that in mind, I think the most important goal here should be to make sure that an explicit extension method invocation like This means that the spec should either continue to make |
I don't see any real issue with allowing it. It doesn't really particularly commit us to |
That would be the meaning, yes. So, seems like we are leaning towards accepting it. I'll update the design document to match. |
It's decided then, we will allow this. |
This PR introduces text corresponding to the decision in language issue #677: It is allowed to use expressions of the form `E(o)?.id` (and similarly for all other kinds of null-aware member access) to access an extension member from the extension `E` for the receiver `o`.
Specification adjusted in #953. It is already supported by implementations, so I'll close the issue. |
See dart-lang/sdk#39325 for context.
The specification of extension methods states that
Ext(c)?.foo
is disallowed because it treatsExt(c)
as a value in order to check whether it'snull
.The language implementations (front-end and analyzer) accepts it as implicitly meaning
(tmp = c) == null ? null : Ext(tmp).foo()
. That is, they treatExt(c)
as having the same value asc
, just with different "members", and applies the?.
to that.That behavior is not unreasonable. It allows users to avoid applying extensions to
null
inline (without a separatex != null ? ...
), even though all extensions currently acceptnull
.If we ever move to "extension types" where you can write
var tmp = Ext(c); tmp.foo();
, it would still make sense to allowtmp?.foo()
because at that pointExt(c)
will have a value: the same value asc
but with a different static only type. Checking the actual value for beingnull
makes sense then, and allowingExt(c)?.foo()
is consistent with that view (evaluatec
to a value, statically cast it to the extension type, then do?.foo()
on that value which might benull
).So, should we keep the currently implemented behavior and amend the specification?
The text was updated successfully, but these errors were encountered: