Skip to content

Type Inference with Generics that extend other generics #45563

Closed
@matejthetree

Description

@matejthetree
  • Dart SDK Version Dart SDK version: 2.12.0 (stable) (Thu Feb 25 19:50:53 2021 +0100) on "macos_x64"
  • MacOSX

In this code the compiler/analyzer does not recognize that foo inside Boo is Foo<String>

void main() {
  var boo = Boo(FooStringer((String val) => print(val)));
  // boo.foo.call('5'); // this works
  boo.call('5');
}

class Foo<V> {
  final void Function(V) call;

  Foo(this.call);
}

class FooStringer extends Foo<String> {
  FooStringer(void Function(String val) callback) : super(callback);
}

class Boo<T extends Foo<V>, V> {
  final T foo;

  void call(V val) => foo.call(val);

  Boo(this.foo);
}

throws type '(String) => void' is not a subtype of type '(dynamic) => void'

changing Boo to this throws the same error

void main() {
  var boo = Boo(FooStringer((String val) => print(val)));
  // boo.foo.call('5'); // this works
  boo.call('5');
}

class Foo<V> {
  final void Function(V) call;

  Foo(this.call);
}

class FooStringer extends Foo<String> {
  FooStringer(void Function(String val) callback) : super(callback);
}

class Boo<T extends Foo> {
  final T foo;

  void call(String val) => foo.call(val);

  Boo(this.foo);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-as-intendedClosed as the reported issue is expected behaviortype-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions