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

List<int>.fold not compiling to javascript because of operator not found #42042

Closed
ravish-tech opened this issue May 25, 2020 · 2 comments
Closed
Labels
closed-as-intended Closed as the reported issue is expected behavior type-question A question about expected behavior or functionality

Comments

@ravish-tech
Copy link

ravish-tech commented May 25, 2020

This can be replicated on dartpad.dev.
When trying to run the below code, I get error. I was expecting this to run and give me 2 as answer.

void main() {
  List<int> a = List<int>();
  a.add(2);
  print(a.fold(0, (prev, v) => prev + v));
}
Error compiling to JavaScript:
main.dart:4:37:
Error: The operator '+' isn't defined for the class 'Object'.
 - 'Object' is from 'dart:core'.
  print(a.fold(0, (prev, v) => prev + v));
                                    ^
Error: Compilation failed.

Based on Dart SDK 2.8.2

@eernstg
Copy link
Member

eernstg commented May 25, 2020

You need to give type inference some extra information:

void main() {
  List<int> a = List<int>();
  a.add(2);
  print(a.fold<int>(0, (prev, v) => prev + v));
}

With the original version, the inferred type argument to fold will be Object, which makes prev a parameter of type Object.

This issue arises because the Dart type inference does not transfer information between parameters in this situation, cf. dart-lang/language#731. It rarely comes up except for fold.

@lrhn lrhn added the type-question A question about expected behavior or functionality label May 26, 2020
@a-siva a-siva added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label May 26, 2020
@sigmundch sigmundch added closed-as-intended Closed as the reported issue is expected behavior and removed area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. labels May 26, 2020
@sigmundch
Copy link
Member

(I believe Erik's comment should address your question, but feel free to reopen if we misunderstood)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

5 participants