M. Walker
M. Walker

Reputation: 613

Returning Class from function

Can someone explain why this does not work?

class Foo {
  Foo(this.foo);
  sayFoo() {
    print(foo);
  }
}

var test = () => Foo;
test()("blah").sayFoo(); // ERROR

Upvotes: 1

Views: 83

Answers (2)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657248

Dart currently doesn't support constructor tear-offs, but it's planned to be added to the language eventually.

test() returns a Type<Foo>, but that doesn't allow you to invoke the constructor.

What you can do to simulate tear-offs is creating a closure

var test = (String foo) => Foo(foo);
test("blah").sayFoo(); // should work now

Upvotes: 3

Randal Schwartz
Randal Schwartz

Reputation: 44056

It doesn't work because it isn't designed to work that way.

Upvotes: -2

Related Questions