Skip to content

Unexpected type error when using a typedef #32641

Closed
@zanderso

Description

@zanderso

When running the following program there is a type error in the assert().

test.dart:

typedef T DoSpecialThing<T>(T thing);

String specialStringThing(String thing) => thing;
double specialDoubleThing(double thing) => thing;

class Property<T> {
  Property(this._operation);
  T doThing(T thing) => thing;
  DoSpecialThing<T> get doSpecialThing => _operation;
  DoSpecialThing<T> _operation;
}

Property<String> stringProperty = new Property<String>(specialStringThing);
Property<double> doubleProperty = new Property<double>(specialDoubleThing);

void main() {
  List<Property> properties = [ stringProperty, doubleProperty ];
  List values = [ "String value", 0.0 ];

  for (int i = 0; i < properties.length; ++i) {
    assert(properties[i].doSpecialThing(values[i]) == values[i]);
  }
}
$ dart --enable-asserts --preview-dart-2 test.dart 
Unhandled exception:
type '(String) => String' is not a subtype of type '(dynamic) => dynamic'
#0      main (file:///usr/local/google/home/zra/dart/dart2tests/test.dart)
#1      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
#2      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)

However, if doSpecialThing is replaced with doThing, which should have the same type(?), there is no type error.

/cc @crelier @leafpetersen

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.os-fuchsia

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions