개요
switch
표현식
패턴 매칭
엄격한 검사
코드가 입력 받을 수 있는 모든 조건을 전부 확인하고 있는지 검사
보호 구문
when 키워드로 보호 구문 추가
when 키워드는 boolean으로 반환할 조건을 각 case문에 추가
when 키워드 뒤에 오는 조건이 true를 반환하지 않으면 case 매치가 되지 않음
예제
코드
void if_test () {
print ( '--- start if_test ---' );
int i = 1 ;
if ( i == 1 ) {
print ( 1 );
} else if ( i == 2 ) {
print ( 2 );
} else {
print ( 'else' );
}
print ( '--- end if_test --- \n ' );
}
void switch_test () {
print ( '--- start switch_test ---' );
int i = 1 ;
switch ( i ) {
case 1 :
print ( 1 );
break ;
case 2 :
print ( 2 );
break ;
default :
print ( 'default' );
break ;
}
print ( '--- end switch_test --- \n ' );
}
void switch_expression_test () {
print ( '--- start switch_expression_test ---' );
int i = 1 ;
String s = switch ( i ) {
1 = > '1' ,
2 = > '2' ,
3 = > '3' ,
_ = > '_' ,
};
print ( s );
print ( '--- end switch_expression_test --- \n ' );
}
void switch_pattern_matching_test ( dynamic any ) {
print ( '--- start switch_pattern_matching_test ---' );
switch ( any ) {
case 1 :
print ( 1 );
break ;
case [ 1 , 2 ] :
print ( '[1, 2]' );
break ;
case [ _ , _ , _ ] :
print ( '[_, _, _]' );
break ;
case [ int i , String s ] :
print ( '[int i, String s]' );
break ;
case ( int i , String s ) :
print ( '(int i, String s)' );
break ;
default :
print ( 'default' );
break ;
}
print ( '--- end switch_pattern_matching_test --- \n ' );
}
void switch_exhaustiveness_checking_test () {
print ( '--- start switch_exhaustiveness_checking_test ---' );
bool ? b ;
switch ( b ) {
case true :
print ( 'true' );
break ;
case false :
print ( 'false' );
break ;
case null :
print ( 'null' );
break ;
}
print ( '--- end switch_exhaustiveness_checking_test --- \n ' );
}
void switch_guard_clause_test () {
print ( '--- start switch_guard_clause_test ---' );
( int i , int j ) value = ( 1 , 2 );
switch ( value ) {
case ( _ , _ ) when value . $ 1 == 1 :
print ( 1 );
break ;
default :
print ( 'default' );
}
print ( '--- end switch_guard_clause_test --- \n ' );
}
void main () async {
if_test ();
switch_test ();
switch_expression_test ();
switch_pattern_matching_test ( 1 );
switch_pattern_matching_test ([ 1 , 2 ]);
switch_pattern_matching_test ([ 1 , 2 , 3 ]);
switch_pattern_matching_test ([ 1 , 'a' ]);
switch_pattern_matching_test (( 1 , 'a' ));
switch_pattern_matching_test (( 1 , 'a' , 3 ));
switch_exhaustiveness_checking_test ();
switch_guard_clause_test ();
}
실행 결과
--- start if_test ---
1
--- end if_test ---
--- start switch_test ---
1
--- end switch_test ---
--- start switch_expression_test ---
1
--- end switch_expression_test ---
--- start switch_pattern_matching_test ---
1
--- end switch_pattern_matching_test ---
--- start switch_pattern_matching_test ---
[ 1, 2]
--- end switch_pattern_matching_test ---
--- start switch_pattern_matching_test ---
[ _, _, _]
--- end switch_pattern_matching_test ---
--- start switch_pattern_matching_test ---
[ int i, String s]
--- end switch_pattern_matching_test ---
--- start switch_pattern_matching_test ---
( int i, String s)
--- end switch_pattern_matching_test ---
--- start switch_pattern_matching_test ---
default
--- end switch_pattern_matching_test ---
--- start switch_exhaustiveness_checking_test ---
null
--- end switch_exhaustiveness_checking_test ---
--- start switch_guard_clause_test ---
1
--- end switch_guard_clause_test ---
Tags:
conditional statement ,
Dart ,
if ,
programming-language ,
switch ,
switch exhaustiveness checking ,
switch expression ,
switch guard clause ,
switch pattern matching
Categories:
Dart ,
programming-language
Updated: May 29, 2024