Closed
Description
typedef T RecognizerCallback<T>();
class Test {
T invokeCallback<T>(RecognizerCallback<T> callback) {
return callback();
}
void callback() {
}
void test() {
invokeCallback<String>(callback);
invokeCallback<Null>(callback);
}
}
╭─ ~/src/dart/sdk ‹master›
╰─$ sdk/bin/dartanalyzer --strong -v t/analysis.dart 3 ↵
Analyzing t/analysis.dart...
Using default analysis options
No issues found!
If I now move the code to top-level scope then analyzer reports an error
typedef T RecognizerCallback<T>();
T invokeCallback<T>(RecognizerCallback<T> callback) {
return callback();
}
void callback() {
}
void test() {
invokeCallback<String>(callback);
invokeCallback<Null>(callback);
}
╭─ ~/src/dart/sdk ‹master›
╰─$ sdk/bin/dartanalyzer --strong -v t/analysis.dart
Analyzing t/analysis.dart...
Using default analysis options
error • The function 'callback' has type '() → void' that isn't of expected type '() → String'. This means its parameter or return type does not match what is expected at t/analysis.dart:13:28 • strong_mode_invalid_cast_function
error • The function 'callback' has type '() → void' that isn't of expected type '() → Null'. This means its parameter or return type does not match what is expected at t/analysis.dart:14:26 • strong_mode_invalid_cast_function
1 error found.
[Flutter is full of this sort of mistyped code (except they use Null
instead of String
) , so once this is fixed we would heed to do a pass over their code and fix it /cc @leafpetersen ]
@devoncarew could you help triage this? Thanks.