Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support declaration & use of generic function types #25179

Closed
jmesserly opened this issue Dec 8, 2015 · 5 comments
Closed

support declaration & use of generic function types #25179

jmesserly opened this issue Dec 8, 2015 · 5 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on

Comments

@jmesserly
Copy link

We have strong mode support for generic methods. Internally we can model types like <T>(T) -> T however you can't write this type in code. Typedefs are pretty close but the implicit-dynamic type args prevent it from working. Here are a few syntaxes we've played with:

// alternative A
typedef /*generic*/ T F<T>(T t);
F f; // can be called like: f<int>(42)

// alternative B
typedef T F<T>(T t);
F/*<>*/ f;

"A" has the benefit that you can declare the type once, and then use it freely. Using it doesn't require understanding much about the type.

"B" has the benefit that it works with existing typedefs, doesn't change Dart's implicit-dynamic rules, and could work for classes too. (There's also precedent in C#).

EDIT:
When implementing this, we need to add some additional logic (to ErrorVerifier?) to "check that bounds (on both classes and generic methods) are not themselves generic function types" and "Check that type arguments are not generic function types.". Splitting that out of #25200, since it's not something we need to do until we implement this bug.

CC @leafpetersen

@jmesserly jmesserly added Priority-Medium area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Dec 8, 2015
@bwilkerson
Copy link
Member

Is there going to be real syntax to support this at some point? If so, do we know what it will look like?

Also, can you give me an example of where a generic function type would be used?

@jmesserly
Copy link
Author

Is there going to be real syntax to support this at some point? If so, do we know what it will look like?

yup! I wasn't clear above, but the comment syntax is temporary while the DEP is under consideration. Real syntax would look similar but without the comments.

This issue on the DEP discusses it in more depth: leafpetersen/dep-generic-methods#10

For example, one place this happens is a first-class generic function and tear-off methods:

main() {
  var list = <int>[1, 2, 3];
  var f = list.map;
  // The type of "f" is now <T>((int) -> T) -> Iterable<T>
}

It would be a little strange if there was no way to annotate that local variable "f" with a type.

@leafpetersen
Copy link
Member

@bwilkerson As it stands at the moment, you can write a function which takes a generic function as a parameter (using the function typed parameter syntax), but you can't write a typed function which returns a generic function, and you can't bind a generic function to an explicitly typed variable or a field.

The two best syntax proposals I've seen so far are basically the uncommented versions of the two approaches that @jmesserly wrote above. I don't have strong feelings yet as to which works out better.

@munificent munificent mentioned this issue Feb 18, 2016
35 tasks
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on and removed Priority-Medium labels Mar 1, 2016
@kevmoo
Copy link
Member

kevmoo commented Apr 12, 2018

@jmesserly @leafpetersen – where are we on this?

@leafpetersen
Copy link
Member

The real syntax was shipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

4 participants