Skip to content

Inline functions parameter type override #30878

Closed
@jacehensley-wf

Description

@jacehensley-wf

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!

Activity

vsmenon

vsmenon commented on Sep 25, 2017

@vsmenon
Member

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

jacehensley-wf commented on Sep 25, 2017

@jacehensley-wf
Author

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

vsmenon

vsmenon commented on Sep 25, 2017

@vsmenon
Member

For A.func? I think that would work.

@leafpetersen

leafpetersen

leafpetersen commented on Sep 26, 2017

@leafpetersen
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

jacehensley-wf commented on Sep 26, 2017

@jacehensley-wf
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

leafpetersen commented on Sep 26, 2017

@leafpetersen
Member

That shows as an unnecessary cast info

Bug. Filed it here #30897 .

added
area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).
on Oct 5, 2017
added
closed-staleClosed as the issue or PR is assumed stale
on Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-staleClosed as the issue or PR is assumed stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dgrove@vsmenon@lrhn@leafpetersen@jacehensley-wf

        Issue actions

          Inline functions parameter type override · Issue #30878 · dart-lang/sdk