Skip to content

Compilation error with bounded generic function.Β #59663

@schultek

Description

@schultek

I would expect the following code to compile:

typedef BoundedFunction<R, Bound> = R Function<T extends Bound>(T value);

class BoundedClass<R, Bound> {
  const BoundedClass(this.run);

  final BoundedFunction<R, Bound> run;
}

void main() {
  final BoundedFunction<int, String> boundedFunction = <Y extends String>(Y value) => 1;
  final BoundedClass<int, String> boundedClass = BoundedClass(boundedFunction);
}

However the compiler throws a error at the last line:

lib/fn.dart:13:63: Error: The argument type 'int Function<T extends String>(T)' can't be assigned to the parameter type 'int Function<T extends String>(T)' because 'T' is nullable and 'T' isn't.
  final BoundedClass<int, String> boundedClass = BoundedClass(boundedFunction);
                                                              ^

Not sure if its a bug, but at least the message is very confusing.


dart --version: Dart SDK version: 3.5.0 (stable) (Tue Jul 30 02:17:59 2024 -0700) on "macos_arm64"

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions