-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Description
The problem
Consider the following example:
typedef Action<T> = void Function(T arg);
void execute<T>(T arg, Action<T> action) => action(arg);
void main() {
execute(42, (value) => print(value.bar));
}
I would expect the Dart Analyzer to fail at value.bar
since T
could be inferred to number
. Instead, Dart does not infer T
to number
, but falls back to dynamic
.
What are other languages doing?
Equivalent example in TypeScript:
type Action<T> = (arg: T) => void;
function execute<T>(arg: T, action: Action<T>) {
return action(arg);
}
function main() {
execute(42, (value) => console.log(value.bar));
}
yields error
Property 'bar' does not exist on type 'number'.
Equivalent example in Kotlin:
typealias Action<T> = (arg: T) -> Unit;
fun<T> execute(arg: T, action: Action<T>) = action(arg);
fun main() {
execute(42) { value -> print(value.bar) };
}
yields error
Unresolved reference: bar
https://stackoverflow.com/questions/56540905/type-inference-from-parameter-type
Maistho, mpfaff, sglim, kato1628, canisterism and 7 more
Metadata
Metadata
Assignees
Labels
area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).Dart language related items (some items might be better tracked at github.com/dart-lang/language).