-
Notifications
You must be signed in to change notification settings - Fork 205
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
Generic type information gets lost when directly passed to a function #2314
Comments
I know there was a big change that landed recently that may not have shipped yet (#731), so I may be wrong, but here's my guess: When you declare I guess another way of looking at it is you created this list just to pass it to |
This is working as intended. As @Levi-Lesches says: To do that, it looks both at which type the context expects, and what types the arguments have (elements here, since this is a list, not a function call, but the principle is the same). The type inference algorithm prefers the context type. If there is one, it wins. Only if the context type is not sufficient to infer all the type arguments will the algorithm look at the arguments. So, in this case: In the case of In the case So, working as intended. |
Hi,
I am encountering the following behavior and I am wondering if this is correct.
I have a function that expects an
Iterable<Object>
.Now, when I assign a List without a generic type to a variable declared with
var
, the type is automatically interfered asList<int>
.If I subsequently call the method with this variable, the parameter is also recognized as
List<int>
.In the second case I skip the variable declaration and pass the list directly to the function.
In this case, the type is correctly recognized as a list (instead of iterable), but the generic type is not interfered as
int
.So when passing a list directly, the generic parameter must be specified in the function call for the function to interfere it correctly.
Example:
In my opinion it shouldn't make a difference whether I assign the list to a variable declared with
var
before the function call or pass it directly into the function.Can you please explain to me why the code behaves the way it does and if the behavior is expected? Thanks in advance.
Tested with Dart 2.17.1
The text was updated successfully, but these errors were encountered: