-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Labels
area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.
Description
Dart does not infer the generic type of a callable class from the context, even when the type is unambiguous. For example:
class Validator<T> {
String? call(T? value) => null;
}
TextFormField(validator: Validator()); // T inferred as dynamic (dart 3.10.1)- TextFormField.validator expects String? Function(String?).
- Declaring Validator() works, but is redundant, because any other type produces a compile-time error.
- In theory, the compiler could infer T = String automatically, since it’s the only valid choice.
- Dart already does this inference for closures, but not for callable classes.
This would make callable classes more ergonomic in situations like this.
Is this behavior intended? Why?
Could Dart support inferring the generic type of a callable class from the context?
Metadata
Metadata
Assignees
Labels
area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.