-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Callable classes loose their type when passed as function type #49683
Comments
This is working as intended. We used to have support for custom function objects: An instance of However, that approach was too costly: It introduces infinite types like In order to reduce the amount of breakage to a minimum, we introduced a tiny bit of syntactic sugar: If an expression This means that your example is exactly the same thing as this: import 'package:test/test.dart';
typedef FooFunction = void Function();
class FooObject {
void call() {}
}
String foo(FooFunction foo) {
if (foo is FooObject) {
return 'FooObject';
} else {
return 'FooFunction';
}
}
void main() {
test('test', () {
expect(foo(() {}), 'FooFunction');
expect(foo(FooObject().call), 'FooObject'); // Returns `FooFunction`
});
} I'll close this issue because it is all working as intended. |
For context, original breaking change announcement from Dart 2 times: https://groups.google.com/a/dartlang.org/g/misc/c/hvt6jpiYxsE/m/ZQsrT67ZAQAJ |
Thanks for the quick response! For me this happened with new code and it is a surprising and (I think) inconsistent behavior with the rest of the Dart type system. Maybe it would make sense to deprecated the ability to implement |
Coincidentally this is considered here: dart-lang/language#2399 |
Consider the following minimal test case:
Surprisingly, when calling
foo
with an object, the object looses theFooObject
type and magically becomes aFooFunction
. I would expect the original object type to be preserved as ifFooObject
actually implementedFooFunction
.The text was updated successfully, but these errors were encountered: