-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Comments
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? |
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. |
@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. |
@jmesserly @leafpetersen – where are we on this? |
The real syntax was shipped. |
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:"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
The text was updated successfully, but these errors were encountered: