Description
See dart-lang/sdk#39325 for context.
The specification of extension methods states that Ext(c)?.foo
is disallowed because it treats Ext(c)
as a value in order to check whether it's null
.
The language implementations (front-end and analyzer) accepts it as implicitly meaning (tmp = c) == null ? null : Ext(tmp).foo()
. That is, they treat Ext(c)
as having the same value as c
, 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 separate x != null ? ...
), even though all extensions currently accept null
.
If we ever move to "extension types" where you can write var tmp = Ext(c); tmp.foo();
, it would still make sense to allow tmp?.foo()
because at that point Ext(c)
will have a value: the same value as c
but with a different static only type. Checking the actual value for being null
makes sense then, and allowing Ext(c)?.foo()
is consistent with that view (evaluate c
to a value, statically cast it to the extension type, then do ?.foo()
on that value which might be null
).
So, should we keep the currently implemented behavior and amend the specification?