Skip to content

Support a type for "function that returns T" #26420

Closed
@nex3

Description

@nex3

Right now, there's no way to use Dart's type syntax to express a constraint on the return type of a function without also expressing constraints on its arguments. This makes it impossible to make certain classes of first-class functions strong-mode compatible.

The most notable function that needs this expectAsync() in test, which is widely used when testing asynchronous code. It takes a function (with up to six arguments and no named arguments) and returns a wrapper function with a compatible signature and some extra logic. In order to work with strong mode, it needs the output function to have a reified return type that matches the input function's, but there's no way to do that right now.

I propose that we add a generic parameter to Function representing its return type. That way we could write expectAsync() like so:

Function/*=F*/ expectAsync/*<T, F extends Function<T>>*/(Function/*=F*/ callback) {
  if (callback is _SixArgFunction) {
    return ([_0, _1, _2, _3, _4, _5]) => callback(_0, _1, _2, _3, _4, _5);
  } else if // ...
}

I'm marking this as S1 because it will be a barrier to the use of DDC with the test package once support for that exists (dart-lang/test#414).

Metadata

Metadata

Assignees

No one assigned

    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