Skip to content

Impossible to define field of parameterized type with function type #511

Closed
@rakudrama

Description

@rakudrama

In the following example I would like to declare compareFn as having the type E x E -> int.
Dart offers no way to do this. An attempt to declare the type directly conflicts with the syntax of a local function definition. Local typedefs are not supported.

class Methods<E> {
  final compareFn; // This needs improving

  Methods(bool compare(E a, E b)) : compareFn = compare;

  bool lessThan(E a, E b) => compareFn(a, b) < 0;
}

main() {
  var m = new Methods<int>((int a, int b) => a.compareTo(b));

  print('1 < 2 = ${m.lessThan(1, 2)}');
  print('2 < 2 = ${m.lessThan(2, 2)}');
}

It seems that the tightest bound is a binary function returning int. The best of the poor alternatives is to have a top-level typedef for Dynamic x Dynamic -> int.

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions