2017-04-05 69 views
2

我有幾百行代碼。它的許多小塊具有以下結構:爲什麼F#交互標記這個錯誤?

let soa = 
    election 
    |> Series.observations 
printfn "%A" <| soa 

頻繁發生兩件事情:

1)令人不解的最後一行改爲:

printfn "%A" <| 

,這樣上面的代碼和什麼如下變爲

let soa = 
    election 
    |> Series.observations 
printfn "%A" <| 

let sls = 
    election 
    |> Series.sample (seq ["Party A"; "Party R"]) 
printfn "%A" <| sls 

這發生在上面幾百行我在哪裏在編輯器中編輯文件。

2)發生這種情況時F# Interactive不會標記錯誤。不會生成錯誤消息。但是,如果我嘗試訪問sls我得到的消息:

error FS0039: The value or constructor 'sls' is not defined.

爲什麼一些代碼在編輯器中被刪除的任何想法? (這種情況經常發生)

爲什麼F# Interactive發出錯誤信息?

回答

5

第二個let塊被解釋爲前面printfn的參數,因爲pipe是一個運算符,它爲偏移規則提供了一個例外:運算符的第二個參數不必比第一個參數縮進得更遠。由於第二個let塊不在頂層,而是printfn的論點的一部分,所以它的定義不能在外面訪問。

讓我們嘗試一些實驗:

let f x = x+1 

// Normal application 
f 5 

// Complex expression as argument 
f (5+6) 

// Let-expression as argument 
f (let x = 5 in x + 6) 

// Replacing the `in` with a newline 
f (let x = 5 
    x + 6) 

// Replacing parentheses with pipe 
f <| 
    let x = 5 
    x + 6 

// Operators (of which the pipe is one) have an exception to the offset rule. 
// This is done to support flows like this: 
[1;2;3] |> 
List.map ((+) 1) |> 
List.toArray 

// Applying this exception to the `f` + `let` expression: 
f <| 
let x = 5 
x + 6