6.4.12 단일 패턴 검사(Single pattern check)

6.4.12 단일 패턴 검사(Single pattern check)

컴파일러는 enum 값이 주어진 패턴과 일치하는지 검사하는 match 함수를 제공합니다:

var myTree = Node(Leaf("foo"), Node(Leaf("bar"), Leaf("foobar")));
trace(myTree.match(Leaf(_))); // false
trace(myTree.match(Node(_) | Leaf(_))); // true
trace(myTree.match(Node(Leaf("foo"), _))); // true

이 함수는 패턴이 일치하는지만 검사하므로 가드와 변수 캡처는 사용할 수 없습니다.

match 함수는 주어진 패턴에 대해 단일 case(완전하면 true 반환)와 false를 반환하는 default를 가진 switch와 동등합니다.

var myTree = Node(Leaf("foo"), Node(Leaf("bar"), Leaf("foobar")));
myTree.match(Node(_));
// is equivalent to
switch (myTree) {
  case Node(_):
    true;
  case _:
    false;
}

자세한 내용은 EnumValue API 문서(Haxe 3.2.1 이후)를 참조하세요.

출처: Single pattern check

본문

myTree.match(패턴)은 enum 값이 패턴과 일치하는지 검사하는 함수예요. or 패턴은 사용 가능하지만 가드와 변수 캡처는 불가능합니다. 단일 case + default switch와 동등합니다.

더 알아보기