Skip to content

Meta issue tracking implementation of generic function type syntax in Dart 1.5 #27527

Closed
@leafpetersen

Description

@leafpetersen

This is a tracking bug for implementing support for generic function type syntax (generic typedef) in Dart 1.5.

Proposal: https://gist.github.com/eernstg/ffc7bd281974e9018f10f0cb6cfee4aa

The corresponding individual issues will be listed here:

Description

Dart 2 supports generic methods and generic tear-offs (or anonymous functions). This means that a function can request a function that is generic:

// Takes a function `f` that is generic on T.
void foo(A<T> constructorForA<T>()) {
  A<int> x = constructorForA<int>();
  A<String> x = constructorForA<String>();
}

It is straightforward to add the syntax to the existing syntax for function types for parameters (as was done in the example). However, we don't have an easy way to do the same for fields or local variables:

// Does *not* do what we want it to do:
typedef A<T> ConstructorForAFun<T>();

A f;  // A function that returns an `A<dynamic>`.

We already used the most consistent place for the generic method argument as a template argument to the typedef itself. If we could go back in time, we could change it as follows:

typedef<T> List<T> TemplateTypedef();
TemplateTypedef<int> f;  // A function that returns a List<int>.
TemplateTypedef f;  // A function that returns a List<dynamic>.

typedef List<T> GenericTypedef<T>();
GenericTypedef f;  // A function that is generic.
List<int> f<int>();
List<String> f<String>();

Given that this would be a breaking change we have considered alternatives and are now exploring new typedef syntax altogether. The idea is to provide syntax that can also be used for locals and fields at the same time.

For example:

typedef F = (int) -> void;  // Function from (int) to void.
typedef F<T> = () -> List<T>;  // Template Typedef.
typedef F = <T>(T) -> List<T>;  // Generic function from T to List<T>.

This way we would solve 3 issues at the same time:

  • users often forget the parameter name and expect typedef void F(int) to be a function that takes an int.
  • users can write the function type of fields and locals without needing to write a typedef.
  • of course: we can write the type of a generic function.

While investigating different syntax we found that the following points need consideration:

  • nullability: the syntax must be nullable without too much hassle:
      (int)->int?;  // A function that is nullable, or that returns a nullable integer?
     Problem disappears with <-
     int <- (int)? ; vs int? <- (int) 
  • readability
  • interactions with union types (if we ever want them).

Metadata

Metadata

Assignees

Labels

P1A high priority bug; for example, a single project is unusable or has many test failuresarea-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).language-strong-mode-polish

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions