Skip to content
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

Inline functions parameter type override #30878

Closed
jacehensley-wf opened this issue Sep 25, 2017 · 6 comments
Closed

Inline functions parameter type override #30878

jacehensley-wf opened this issue Sep 25, 2017 · 6 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-stale Closed as the issue or PR is assumed stale

Comments

@jacehensley-wf
Copy link

In Dart 2 I am seeing infos about some typing (uses_dynamic_as_bottom) and I don't know how I would get around it.

Basically I have something like:

new A()
  ..func = (int a) {};

Where A.func is of typeFunction(dynamic a)

If I make it Function(Object a) then it becomes an error:

"A value of type '(int) → Null' can't be assigned to a variable of type 'Function(Object) → dynamic'."

I put together a dartpad of a few things to try and make something work and clarify some rules for myself, but I couldn't find a way to make inline functions like that work.

https://dartpad.dartlang.org/22a4849f9f0d2fcd3813024df252adf0

Thanks!

@vsmenon
Copy link
Member

vsmenon commented Sep 25, 2017

We took a mis-step here. Our intent is to make dynamic in your example work like Object - i.e., a static error.

Details here:

#29630

@jacehensley-wf
Copy link
Author

Oh okay, that makes sense. So should I type the parameters to Null?

@vsmenon
Copy link
Member

vsmenon commented Sep 25, 2017

For A.func? I think that would work.

@leafpetersen

@leafpetersen
Copy link
Member

Making A.func typed to take Null will allow any unary function to be assigned to it. Note though that you will need to cast it to a different function type (or use a dynamic call) to call it then with anything except Null. That is:

Function(Null) f = (int x) => x + 1;
f(3); // This will fail, because of an implied downcast of `3` to `Null`
(f as Function(int))(3); // This will work
(f as dynamic)(3); // This will work.

@jacehensley-wf
Copy link
Author

you will need to cast it to a different function type

That shows as an unnecessary cast info


So is there no "bottom" type that isn't null? Something like "Anything"? Because typing the parameter to Object doesn't work and neither does dynamic.

Function(Object) f2 = (int x) => x + 1; // warning
f2(3);
(f2 as Function(int))(3);
(f2 as dynamic)(3);
  
Function(dynamic) f3 = (int x) => x + 1; // info
f3(3);
(f3 as Function(int))(3);
(f3 as dynamic)(3);

@leafpetersen
Copy link
Member

That shows as an unnecessary cast info

Bug. Filed it here #30897 .

@dgrove dgrove added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Oct 5, 2017
@lrhn lrhn closed this as completed Jan 12, 2024
@lrhn lrhn added the closed-stale Closed as the issue or PR is assumed stale label Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-stale Closed as the issue or PR is assumed stale
Projects
None yet
Development

No branches or pull requests

5 participants