-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Different inference with = null, = [null], = {1: null} #32978
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
|
EDIT: Jonah clarified this occurs on the standalone VM with |
This is also on the dart vm with --preview-dart-2. |
To be fair, what about: class Container<T> {}
main() {
// I assume we expect this to print `Container<Null>`, right?
print(new Container(null));
} |
The issue is that to me, and probably a lot of other users, the value |
tl;dr We could lint mutable lists/maps with implicit type argument One case to keep in mind is this one: void foo<T>([List<T> xs = const []]) {} where the default value So the type argument However, it would make sense to have a lint for a mutable list/map literal whose type argument is inferred to be It wouldn't be hard to take another route and make the inferred type argument |
@eernstg That's a good point about mutable/immutable lists/maps, except: // Now we have a mutable List<Null> again :)
final list = const [null].toList(); |
@eernstg inferring dynamic doesn't seem that surprising to me, but that could just be my Dart 1 bias. It seems more consistent with inference for fields/locals. In cases where |
tl;dr Could again be: We might lint mutable lists/maps with implicit type argument @matanlurey, with final list = const [null].toList(); I just can't help thinking: "It's not unreasonable for an inference procedure to decide that you want a But I guess you're saying that it would be more useful if mutable collections with inferred type argument Moreover, it's a two-edged sword: Such a It's really not obvious to me that you always want the |
@jonahwilliams, I agree that we don't want a |
Hmm, looking at this ticket again, I think it could use a better explanation of the fallout from picking Null. For instance, if we take:
then having a "no-implicit-null" is a matter of getting better error localization. Currently you'll get an error on line 3 about some type not being assignable to null, and that gets to @jonahwilliams's point about Null being a fairly unintuitive type. A new user will probably have an issue figuring out that they need to change line 1. So, using "no-implicit-null" (or some other flag that includes that behavior) will point the user in a better direction in this case, and hopefully they can figure out that they need to write In a more complicated case it can get worse though without implicit null.
I'll first make a case for why this is extra bad. In this example, we have a case for making generics invariant. I will now say though why its not necessarily a big deal: most people would never write
unless they had some branch in which they first put items in
So I think its important to frame this problem in terms of its end effect. And I think the end effect will in the vast majority of the time be limited to "that error could have been better" rather than "I can't catch a certain class of bugs." FWIW, there are other ways to make the error better which maybe don't depend on a flag. One hypothetical error message:
I think @jmesserly has done work to provide these types of error messages, though it would require changes to the IDE experience (because they often take multiple lines) and I imagine that POC was not implemented in the CFE. I welcome other examples of how inferring Null can kind of snowball into a bigger issue, because if I'm missing cases where it can, then that would help us figure out what the best solutions are and how important it is that we find such solutions! |
@MichaelRFairhurst I think #33119 is the actual problem here |
Uh oh!
There was an error while loading. Please reload this page.
Forked from an internal discussion between @MichaelRFairhurst and @jonahwilliams.
It looks like this infers
x
asdynamic
:But both of these infer
x
asNull
:Our guess is this is likely never what the user intends, however. @leafpetersen - thoughts?
(@MichaelRFairhurst also brought up
var x = (() => null)();
)The text was updated successfully, but these errors were encountered: