Skip to content

Instantly share code, notes, and snippets.

@chalin
Last active June 21, 2017 16:19
Show Gist options
  • Save chalin/25ea9516adbbba889f4ed186a89c0110 to your computer and use it in GitHub Desktop.
Save chalin/25ea9516adbbba889f4ed186a89c0110 to your computer and use it in GitHub Desktop.
Exploring the new Function-keyword based anonymous function-type syntax
typedef FooOrig<T>(T a);
typedef FooNew1 = T Function<T>(T a);
typedef FooNew2<T> = T Function(T a);
typedef FooNew3<T> = T Function<T>(T a);
// DECLARATION // STATIC TYPE
FooOrig vf_orig0; // (dynamic) → dynamic
FooOrig<int> vf_orig1; // (int) → dynamic
T Function<T>(T a) vf_new; // <T>(T) → T
FooNew1 vf_new1a; // <T>(T) → T
// FooNew1<int> vf_new1b; // error [1]
FooNew2 vf_new2a; // (dynamic) → dynamic
FooNew2<int> vf_new2b; // (int) → int
FooNew3 vf_new3a; // <T>(T) → T
FooNew3<int> vf_new3b; // <T>(T) → T
T id<T>(T x) => x;
int plus1(int i) => i + 1;
void main(List<String> arguments) {
vf_orig0 = main;
vf_orig1 = plus1;
// vf_new = main; // error [2]
vf_new = id;
// vf_new = id<int>; // syntax not supported
vf_new2b = plus1;
// vf_new3b = main; // error [2]
}
// [1]: The type 'FooNew1' is declared with 0 type parameters, but 1 type arguments were given.
// [2]: error: A value of type '(List<String>) → void' can't be assigned to a variable of type '<T>(T) → T'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment