Language/Dart

[Exercism] Two Fer #Optional positional parameters

paran21 2022. 8. 18. 16:18

 

https://exercism.org/tracks/dart/exercises/two-fer

 

Two Fer in Dart on Exercism

Can you solve Two Fer in Dart? Improve your Dart skills with support from our world-class team of mentors.

exercism.org

 

다트는 optional positional parameters를 사용할 수 있다.

 

이렇게 default 값을 넣어 줄 수도 있고

String twoFer([String name = 'you']) {
  return 'One for ${name}, one for me.';
}

아니면 널을 허용한 상태(String?)에서 null일 경우를 분기 처리해주는 방법도 있다.

String twoFer([String? name]) {
  if (name == null) {
    return 'One for you, one for me.';
  } else {
    return 'One for ${name}, one for me.';
  }
}