Dart #14 | Typedef

HyeonWooGa·2023년 10월 8일
0

Dart

목록 보기
15/25
post-thumbnail

Typedef


  • Typedef 개요
  • Typedef 사용

개요

  • 자료형이 헷갈릴 때 도움이 될 자료형 Alias(별명) 를 만드는 방법
  • 배열, Maps 그리고 Sets 의 자료형을 정의할 때 보통 사용합니다.

사용

// Typedef
//// 자료형이 헷갈릴 때 도움이 될 자료형 Alias(별명) 를 만드는 방법
//// 기본적으로 typedef 를 TS 처럼 코드 최상단이나 Typedefs 파일에
//// 한 눈에 보이게 사용하면 좋을 것 같습니다.
//// 현재는 학습 중이므로 코드 중간중간에 사용했습니다.

List<int> reverseListOfNumbers1(List<int> numbers) {
  var reversed = numbers.reversed;  // Iterable
  return reversed.toList();  // List
}

typedef ListOfInts = List<int>; // ListOfInts == List<int>

ListOfInts reverseListOfNumbers2(ListOfInts numbers) => numbers.reversed.toList();

typedef SetOfInts = Set<int>;

SetOfInts addFiveToSetOfNumbers(SetOfInts numbers) {
  numbers.add(5);
  
  return numbers;
}

typedef MapOfCouple = Map<String, String>;

String introduceCouple(MapOfCouple couple) => '${couple['man']} and ${couple['woman']} are a very beautiful couple.';

void main() {
  ListOfInts list = [1, 2, 3, 4];
  SetOfInts set = {1, 2, 3, 4};
  MapOfCouple map = {
    'man': 'Park',
    'woman': 'Lee',
  };
  
  print(reverseListOfNumbers1(list));  // [4,3,2,1] 출력
  print(reverseListOfNumbers2(list));  // [4,3,2,1] 출력
  
  print(addFiveToSetOfNumbers(set));   // {1, 2, 3, 4, 5} 출력
  
  print(introduceCouple(map));         // 'Park and Lee are a very beautiful couple.'
}

학습 중에 작성된 내용이므로 잘못되거나 부족한 내용이 있을 수 있습니다.

profile
Aim for the TOP, Developer

0개의 댓글