2015-11-07 63 views
2

我有以下代碼:沒有分配給現場

type Client = 
     { Name : string; Income : int ; YearsInJob : int 
     UsesCreditCard : bool; CriminalRecord : bool } 

type QueryInfo = 
     { Title  : string 
     Check  : Client -> bool 
     Positive : Decision 
     Negative : Decision } 

    and Decision = 
     | Result of string 
     | Querys of QueryInfo 

let tree = 
     Querys {Title = "More than €40k" 
       Check = (fun cl -> cl.Income > 40000) 
       Positive = moreThan40 
       Negative = lessThan40} 

但在最後一行:

Querys {Title = "More than €40k" 
        Check = (fun cl -> cl.Income > 40000) 
        Positive = moreThan40 
        Negative = lessThan40} 

我有一個erorr:

No assignment has given for field 'Check' of type 'Script.QueryInfo' 

回答

6

F#是空白敏感的,這意味着空白用於指示範圍。給出的代碼不能編譯,因爲Check顯得太靠左了。

此,在另一方面,應該編譯(如果moreThan40lessThan40正確定義):

let tree = 
     Querys {Title = "More than €40k" 
       Check = (fun cl -> cl.Income > 40000) 
       Positive = moreThan40 
       Negative = lessThan40} 

的花此處括號並不表明範圍,而是記錄的開始和結束。由於OP中的縮進不正確,因此編譯器將綁定視爲不在記錄表達式的範圍之內。這就是它抱怨沒有價值被綁定到該領域Check的原因。

直到你習慣了重要的空白區域,它可能有點煩人,但它確實幫你省去了很多明確的開啓和關閉範圍(例如花括號),所以在我看來,一旦你習慣了它,這是一個好處。