Skip to content

infer constructor type arguments from values #25220

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

Closed
kasperpeulen opened this issue Dec 10, 2015 · 9 comments
Closed

infer constructor type arguments from values #25220

kasperpeulen opened this issue Dec 10, 2015 · 9 comments
Assignees
Labels
legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@kasperpeulen
Copy link

Would it be possible that strong mode infers the type of otherMap below to be Map<String, String>

  var map = new Map<String, String>();   
  var otherMap = new Map.from(map);

Now otherMap has type Map<dynamic, dynamic>.

@kasperpeulen kasperpeulen changed the title strong mode inferring generic type arguments of types of passed value strong mode: inferring generic type arguments of types of passed value Dec 10, 2015
@jmesserly jmesserly added the legacy-area-analyzer Use area-devexp instead. label Dec 11, 2015
@jmesserly
Copy link

@kasperpeulen definitely! We can use upwards inference for constructors like we do for generic methods. Was chatting with @leafpetersen earlier this week about this.

@kasperpeulen
Copy link
Author

awesome 👍

@jmesserly
Copy link

BTW, a related issue is that we don't (AFAIK) do downwards inference on generic methods. Current state of the world:

  • constructor type parameters are only inferred from downwards inference
  • generic function type parameters are only inferred from upwards inference

@jmesserly
Copy link

Also map/list literals: dart-archive/dev_compiler#108
and block bodied lambda return: #25487

@jmesserly
Copy link

note, we should fix LUB first: #25802, or we're going to get strange results in some cases

@jmesserly jmesserly self-assigned this Feb 19, 2016
@jmesserly
Copy link

https://codereview.chromium.org/1717803003/ fixes literals. Still need constructors.

@jmesserly
Copy link

https://codereview.chromium.org/2115173002/ fixes constructors

pq added a commit that referenced this issue Jul 18, 2016
* Support for trailing commas in parameter and argument lists (#26647).
* Strong mode breaking change: can now infer generic type arguments from the constructor invocation arguments (#25220).

BUG=
[email protected]

Review URL: https://codereview.chromium.org/2163503002 .
@kasperpeulen
Copy link
Author

@jmesserly Not sure if I understand correctly, but it doesn't seem to work as I expected:
image

@jmesserly
Copy link

jmesserly commented Feb 25, 2017

@kasperpeulen the Map.from constructor (like List.from) is designed to be able to cast from one type to another, so it won't infer.
The signature is new Map.from(Map other)
if the signature was new Map.from(Map<K, V> other) then it would work, but you would not be able to use Map.from in a cast like this:

var m = <dynamic, dynamic>{1: 2}
var m2 = new Map<int, int>.from(m); // allowed, casts each key/value

that's an API design choice for the SDK. Inference however is supported for constructors. For example List.filled can infer from the value type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants