dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.21k stars 1.57k forks source link

Generics error with typedef #48898

Closed jodinathan closed 2 years ago

jodinathan commented 2 years ago

Can't seem to be able to use typedef and generics while it works when using the full function signature.

Dart SDK version: 2.16.2 (stable) (Tue Mar 22 13:15:13 2022 +0100) on "macos_x64"

class Foo {}

typedef Bar<T extends Foo> = void Function(T);

class Daz<T extends Foo> {
  void load(T t) {}

  void fus(void Function(T) fn) {}

  void foo(Bar fn) {}

  void daz() {
    fus(load); // OK
    foo(load); // error
    foo(load<T>); // error
  }
}
mraleph commented 2 years ago

Bar means Bar<Foo> (instantiate to bounds). You mean to write Bar<T> in foo signature:

  void foo(Bar<T> fn) {}

Then foo(load) will compile.

load<T> does not compile because load is not a generic function itself so it expects no type arguments of its own.