-
Notifications
You must be signed in to change notification settings - Fork 209
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
Allow == null
to promote to Null
#1959
Comments
There really is no necessary use-case. Except possibly the case of promoting a type-variable typed variable to |
@lrhn yeah, honestly I'm really torn about this one. On the one hand, On the other other hand, I'm not sure "just write the damn Ok, I'm out of hands now. I may play with this just out of curiosity, to see how difficult it would be to implement (I suspect it's very easy), and to double check whether there are any unintended consequences. But it definitely seems like a low priority improvement at best. |
I was just reminded that this issue has come up before, and it proved to have more complex consequences than we expected: #1505 |
On the web compilers, v == null ? v : f(v) is not the same as
since there are two JavaScript values ( This matters for js-interop. We must allow the programmer to copy a null value from one external call to another without changing the 'bit pattern' of the null value. Since we cannot avoid multiple |
@rakudrama Oh, that's very interesting. I love the idea of multiple As for the idea of multiple |
I'd bet that there will be situations where that too would be wrong, and you would want to definitely send the Dart |
I want to ensure that the language specification is not unfriendly towards the reality of multiple Imagine that js-interop is used, in effect, to store a JavaScript value in a Dart Map under some well-behaved Dart key. It would be incorrect if any code involved was to 'convert' the One way that an unwanted conversion might happen is if the promotion-to-Null of |
I'm a little lost as to where the discussion is going here. Nothing in this proposal touches on semantics, and whether compilers do or do not replace values of type In general, I'm suspicious of claims that we should not reflect things that are blatantly obvious to the programmer into the type system: if a user writes |
I did some investigation of what the consequences would be of allowing The TL/DR is, I don't see a big benefit to allowing |
In my investigation of #1618, I ran into several instances in not-yet-migrated code of conditional expressions like this:
v == null ? v : f(v)
(wherev
is a local variable andf(v)
is some complex expression involvingv
. Prior to null safety, this generally works because ifv
andf(v)
have unrelated types, the type of the conditional expression isObject
, andObject
can be implicitly downcast to any other type. But when this pattern is migrated to null safety, it almost always leads to a compile-time error, because null safe code only allows implicit downcasts fromdynamic
.Users can of course work around this problem by changing to
v == null ? null : f(v)
. But @lrhn points out that if we allowed a successfulv == null
test to promote the type ofv
toNull
, they wouldn't need to.Off the top of my head, I can't think of any reason why we shouldn't allow this promotion. (I suspect the only reason it didn't get implemented as part of flow analysis in the first place is because we couldn't think of a use case for it).
The text was updated successfully, but these errors were encountered: