2013-08-09 2 views
5

연결 일치식이 컴파일되지 않습니다. (첫 번째 일치식이 paranthesis에 임) 이전에 일하지 않는 동안 체인 일치식이 컴파일되지 않는 이유는 무엇입니까?

(x match { 
    case Array("abc", _*) => Some("abc is first") 
    case Array("xyz", _*) => Some("xyz is first") 
    case _ => None 
}) match { 
    case Some(x) => x 
    case _ => "Either empty or incorrect first entry" 
} 

이 왜 최신 버전 잘 컴파일 않습니다

:
val x = Array("abc", "pqr") 

x match { 
    case Array("abc", _*) => Some("abc is first") 
    case Array("xyz", _*) => Some("xyz is first") 
    case _ => None 
} match { 
    case Some(x) => x 
    case _ => "Either empty or incorrect first entry" 
} 

다음 잘 컴파일하는 동안

? 이 허용 된 경우

+3

없다, 그러나 키워드 및 컴파일러는 이러한 방식으로 작동합니다. – senia

+0

@senia 항상 할 말이 있습니다. 물론 항상 독창적이거나 재미있는 것은 아닙니다. –

+0

@senia 이미 답변을했다면 대답이 없었을 것입니다. –

답변

1

, 당신은 할 수 없었 : 그것은 중위 표기법 인 경우

scala> List(1,2,3) last match { case 3 => true } 
warning: there were 1 feature warning(s); re-run with -feature for details 
res6: Boolean = true 

, 다음, 왼쪽에있는 것은 후위 수 없습니다 것을.

중위 일치를 허용하지 않으면 후위 수색이 가능합니다. 식을 후위 표기법 자연과 신성이 아닌 경우이며, 자연적인 방법

(List(1,2,3) last) match { case 3 => true } 

를 해석됩니다

.

기능 경고는 import language.postfixOps입니다. 아마도 그 기능이 해제 된 상태에서 수호자는 import language.infixMatch을 기꺼이 기꺼이 기꺼이받을 것입니다.

는 괄호없이 infixable하지 match에 구문 형제입니다 구조, 고려 : match`는 방법이 아닙니다 '를 제외하고 여기 말할 것도

scala> if (true) 1 else 2 match { case 1 => false } 
res4: AnyVal = 1 // not false 

scala> (if (true) 1 else 2) match { case 1 => false } 
res1: Boolean = false 

또는

scala> throw new IllegalStateException match { case e => "ok" } 
<console>:11: error: type mismatch; // not "ok", or rather, Nothing 
found : String("ok") 
required: Throwable 
       throw new IllegalStateException match { case e => "ok" } 
                   ^

scala> (throw new IllegalStateException) match { case e => "ok" } 
java.lang.IllegalStateException 
관련 문제