참고URL : https://www.tutorialspoint.com/dart_programming/dart_programming_typedef.htm

 

Dart Programming - Typedef

Dart Programming - Typedef A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function. Given below are the steps to implement typedefs in a Dart

www.tutorialspoint.com

[구글번역]

typedef 또는 함수 유형 별칭은 메모리 내에서 실행 가능한 코드에 대한 포인터를 정의하는 데 도움이 됩니다 . 간단히 말해서 typedef 는 함수를 참조하는 포인터로 사용할 수 있습니다.

다음은 Dart 프로그램에서 typedef 를 구현하는 단계 입니다.

1단계: typedef 정의

typedef 는 특정 기능과 일치시키려는 기능 시그니처를 지정하는 데 사용할 수 있습니다 . 함수 서명은 함수의 매개변수(유형 포함)에 의해 정의됩니다. 반환 유형은 함수 서명의 일부가 아닙니다. 구문은 다음과 같습니다.

typedef function_name(parameters)

2단계: typedef 변수에 함수 할당

typedef 의 변수는 typedef 와 동일한 서명을 가진 모든 함수를 가리킬 수 있습니다 . 다음 서명을 사용하여 typedef 변수에 함수를 할당할 수 있습니다.

type_def  var_name = function_name

3단계: 함수 호출

typedef 변수는 함수를 호출하는 데 사용할 수 있습니다 . 다음은 함수를 호출하는 방법입니다.

var_name(parameters) 

 

[샘플소스] 구글에 검색되는 블로그 들을 참고해서 샘플을 작성해 봄

void main() { 
  
   calculateOper(1,2, add);
   calculateOper(3,1, subtract);
  
  ManyOperation oper = add;
  
  oper(1,2);
  
  oper = subtract;
  
  oper(3,2);
  
 
}

void add(int x, int y){
  print('x+y = ${x+y}');
}

void subtract(int x, int y) {
  print('x-y = ${x-y}');
}

typedef ManyOperation(int x, int y);

void calculateOper(int x, int y, ManyOperation oper){
  oper(x,y);
}

다트패드로 돌려본 결과

+ Recent posts