Created
October 11, 2011 00:20
-
-
Save oogatta/1276943 to your computer and use it in GitHub Desktop.
[Dart] kinds of function definition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ノーマルな形 | |
// ※ところで今の仕様書に FunctionDeclaration っていう Production が無いのはなぜ? | |
int func(int a) { | |
return a; | |
} | |
// FunctionExpression を使う場合。 | |
// Dart に Function Type はあるけど (T) -> <T> っていうよくわからん型なので、 | |
// FunctionTypeAlias (エイリアス)を定義する。らしい。 | |
typedef int A(int a); | |
final A func2 = int _func2(int a){ | |
return a; | |
}; | |
// "(int) -> <int>" という型は int に入らないと怒られるが、強引に演算は進む。 | |
final int func3 = int _func3(int a) { //[Runtime Error] (int) -> <int> is not assignable to int. | |
return a; | |
}; | |
// ただし Dart で型は完全にオプションなのでこれは OK 。 | |
final func4 = (a) => a; | |
main() { | |
print(func(1)); //1 | |
print(func2(1)); //1 | |
print(func3(1)); //1 [Runtime Error] func3 is not a method in null. | |
print(func4(1)); //1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment