2015-06-09 63 views
1

我正在嘗試使用as-pattern來分解元組值,並觀察一些奇怪的解析/鍵入行爲。例如FSI(4.0)運行:在F中解析/鍵入as-patterns#

> let t = (0, (1, 2), 3) in let (_, (_, _) as pair, _) = t in();; 

    let t = (0, (1, 2), 3) in let (_, (_, _) as pair, _) = t in();; 
    -------------------------------------------------------^ 

stdin(9,56): error FS0001: Type mismatch. Expecting a 
    ('a * ('b * 'c)) * 'd 
but given a 
    int * (int * int) * int 
The type ''a * ('b * 'c)' does not match the type 'int' 

然而,例如工作,如果我把一些額外的():

> let t = (0, (1, 2), 3) in let (_, ((_, _) as pair), _) = t in();; 
val it : unit =() 

這是我稍微愚蠢,或者是解析器在第一失常例?我猜想它試圖將「as pair」應用於「,(,_)」,這看起來稍微比看起來更加貪婪。

回答

4

,as更高的優先級,這就是爲什麼你需要額外的()

> let t = (0, (1, 2), 3) in let (_, ((_, _) as pair), _) = t in pair;; 
val it : int * int = (1, 2) 

你可以從錯誤中看到這一點:

stdin(9,56): error FS0001: Type mismatch. Expecting a ('a * ('b * 'c)) * 'd but given a int * (int * int) * int

它會告訴你基本上是放錯地方一些括號;)