Dart-02: Language Tour
Dart-02-01   Important concepts
Dart-02-02   Keywords
Dart-02-03   Variables
Dart-02-04   Built-in types
Dart-02-05   Functions
Dart-02-06   Operators
Dart-02-07   Control flow statements
Dart-02-08   Exceptions
Dart-02-09   Classes
Dart-02-10   Generics
Dart-02-11   Libraries and visibility
Dart-02-12   Asynchrony support
Dart-02-13   Generators
Dart-02-14   Callable classes
Dart-02-15   Isolates
Dart-02-16   Typedefs
Dart-02-17   Metadata
Dart-02-18   Comments

Typedefs

Dart - Typedefs

typedef keyword로 선언되기 때문에 typedef로 불리는 type alias는 type을 참조하는 간결한 방법이다. 다음은 IntList라는 type alias를 선언하고 사용하는 예이다:

typedef IntList = List<int>;
IntList il = [1, 2, 3];

type alia에는 type parameter가 있을 수 있다.

typedef ListMapper<X> = Map<X, List<X>>;
Map<String, List<String>> m1 = {};  // Verbase.
ListMapper<String> m2 = {}; // Some thing but shorter and clearer.

2.13 version에는 typedef가 함수 type으로 제한되었다. 새로운 typedef를 사용하려면 최소 2.13의 language version이 필요하다.

대부분의 상황에서 함수에 대한 typedef 대신 inline function type을 사용하는 것이 좋다. 그러나, 함수 typedef는 여전히 유용할 수 있다.

typedef Compare<T> = int Function(T a, T b);

int sort(int a, int b) => a - b;

void main() {
  assert(sort is Compare<int>); // True!
}

태그:

카테고리:

업데이트:

댓글남기기