Type inference for simple generic method requires explicit type

Viewed 31

I'm trying to understand if and how I can make the automatic type inference for a simple generic method work without having to explicitly provide a type anywhere.

For example, the following code doesn't compile and shows an error that "The argument Object? can't be assigned to the parameter type num":

void test<V>(V value, Function(V value) doStuff){
  doStuff(value);
}


test(12, (v) => print('${10*v}'));
                            ^                            
         The argument Object? can't be assigned to the parameter type num

I have to do either

test(12, (int v) => print('${10*v}'));

or

test<int>(12, (v) => print('${10*v}'));

in order to make it work.

Why doesn't the Dart analyzer see that since 12 is an int, v must also be an int? Can I modify test() so that the automatic type inference understands that v is an int?

0 Answers
Related