왜 이것이 작동하지 않는지 궁금합니다.
def example(list: List[Int]) = list match {
case Nil => println("Nil")
case List(x) => println(x)
}
example(List(11, 3, -5, 5, 889, 955, 1024))
그것은 말한다 :
scala.MatchError: List(11, 3, -5, 5, 889, 955, 1024) (of class scala.collection.immutable.$colon$colon)
List(x)
정확히 하나의 요소가있는 목록을 의미 하기 때문에 작동하지 않습니다 . 확인해 봐:
def example(list: List[Int]) = list match {
case Nil => println("Nil")
case List(x) => println("one element: " + x)
case xs => println("more elements: " + xs)
}
example(List(11, 3, -5, 5, 889, 955, 1024))
//more elements: List(11, 3, -5, 5, 889, 955, 1024)
example(List(5))
//one element: 5
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다