Skip to content

How do I pass a class as a function parameter? #40923

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

Closed
Mrooze-zeng opened this issue Mar 9, 2020 · 1 comment
Closed

How do I pass a class as a function parameter? #40923

Mrooze-zeng opened this issue Mar 9, 2020 · 1 comment
Labels
type-question A question about expected behavior or functionality

Comments

@Mrooze-zeng
Copy link

Mrooze-zeng commented Mar 9, 2020

Hi guys, I am new to dart. Can you help me how to pass the class as a function parameter?
For example, the code in Javascript can be written like this:

class MyCls{
  hello(){}
}

function myFun(cls){
  return new cls();
}

myFun(MyCls);

but how do I pass a class as a function parameter in dart?

@mraleph mraleph added the type-question A question about expected behavior or functionality label Mar 9, 2020
@mraleph
Copy link
Member

mraleph commented Mar 9, 2020

You can't pass a class as a function in Dart, because classes are not functions.

Right now you need to explicitly create a closure:

class MyCls {
}

myFun(maker){
  return maker();
}

myFun(() => MyCls());

Though there are some outstanding feature requests for some sort of constructor tear-off syntax (e.g. dart-lang/language#216 and dart-lang/language#691).

@mraleph mraleph closed this as completed Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

2 participants