为什么F#interactive没有标记这个错误?

时间:2017-04-05 18:32:54

标签: visual-studio compiler-errors f# f#-interactive

我有几百行的代码。它的许多小块具有以下结构:

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没有发出错误消息?

1 个答案:

答案 0 :(得分:5)

第二个let块被解释为前面的printfn的参数,因为作为运算符的管道为偏移规则提供了一个例外:运算符的第二个参数不必比第一个参数缩进得更远。由于第二个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