Skip to content
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

Compilation error on OneLiner #36405

Closed
Segebre opened this issue Apr 1, 2019 · 2 comments
Closed

Compilation error on OneLiner #36405

Segebre opened this issue Apr 1, 2019 · 2 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report

Comments

@Segebre
Copy link

Segebre commented Apr 1, 2019

When using fold inside a print() we get a compilation error for the + inside the combine function. This happens on DartPad and can be shown here.

@lrhn
Copy link
Member

lrhn commented Apr 1, 2019

The code in question is

var nums = [1, 4, 2, 3, 5];
print(nums.fold(-10, (p, i) => p + i)); // Uncommenting this causes a compilation error.

And the error is that + is not defined on Object.
I'm guessing downards inference pushes down Object as the return type of fold, which then means that the type of p becomes Object.
The signature of fold is:

class Iterable<E> {
   T fold<T>(T intiialValue, T Function(T, E) combine));
}

This is technically working as intended, because the downwards inference wins. It's just a useless inference here, and a place where upwards would give the correct result.

@lrhn lrhn added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Apr 1, 2019
@eernstg
Copy link
Member

eernstg commented Apr 1, 2019

Also note #25490 and #34118, where it is discussed that fold in particular tends to create situations where the current approach to inference does not route the available information far enough to produce the best outcome. Changes in this area need to be made cautiously because they are likely to be pervasively but subtly breaking, but it is certainly on the radar.

I believe the consistent approach would be to close this issue as a duplicate of #25490. I've added a reference to this issue in #25490 in order to preserve the information that it came up again, and how.

@eernstg eernstg closed this as completed Apr 1, 2019
@eernstg eernstg added the closed-duplicate Closed in favor of an existing report label Apr 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

3 participants