2016-09-19 82 views
1

使用Visual Studio 2015 Update 3及fsi.exe我試圖運行此腳本:#如果 - 的#else - #ENDIF從F#4.0版打破F#腳本

//error.fsx 

#if INTERACTIVE 
    let msg = "Interactive" 
#else 
    let msg = "Not Interactive" 
#endif 

let add x y = 
    x + y 

printfn "%d" (add 1 2) 

輸出:error.fsx (12,15):錯誤FS0039:價值或構造的 '添加' 沒有定義

如果我再註釋掉#if - #else - #endif塊,它工作正常:

// fixed.fsx 

//#if INTERACTIVE 
// let msg = "Interactive" 
//#else 
// let msg = "Not Interactive" 
//#endif 

let add x y = 
    x + y 

printfn "%d" (add 1 2) 

輸出:

我敢肯定,我做錯了什麼(而不是這個是一個錯誤),但我不能爲我的生活弄清楚如何使這一工作。

想法?

回答

4

這是由let msg的縮進造成的。以下工作正常:

#if INTERACTIVE 
let msg = "Interactive" 
#else 
let msg = "Not Interactive" 
#endif 

let add x y = 
    x + y 

printfn "%d" (add 1 2) 

我必須說,我不完全知道爲什麼代碼會正是你提到的錯誤信息 - 我懷疑這是在錯誤報告中的錯誤,它沃爾德值得reporting it to the F# team。至少應該有一個明智的錯誤信息!

這似乎與縮進時,F#編譯器實際上解析代碼如下:

let msg = "Interactive" 
let add x y = 
    x + y 
    printfn "%d" (add 1 2) 

的間距let msg之前使編譯器把printfn爲被縮進了。如果您使用工具提示在編輯器中查看y變量的類型,則可以看到這種情況...

+0

感謝Tomas!只要你指出這是有道理的。我從[這裏](http://brandewinder.com/2016/02/02/06-fsharp-scripting-tips/)抓取了示例代碼(#3),並假定它能夠像這樣工作。非常感謝! –

+0

這是一個有趣的錯誤 - 我花了一點時間來弄清楚發生了什麼,我認爲這是一個錯誤(至少在錯誤報告中)。 –

+0

馬特克萊恩:對不起!我會修復這個帖子,可能有些格式化會在那裏發生。 – Mathias