2011-05-31 76 views
11

我有這段代碼在文件不存在時引發錯誤。F的文件檢查代碼#

if !File.Exists(doFile) then 
    printfn "doFile doesn't exist %s" doFile; failwith "quit" 

但是,我得到了這個錯誤。怎麼了?

error FS0001: This expression was expected to have type 
    bool ref  
but here has type 
    bool 

回答

17

!運營商在F#有特殊的意義,它的定義爲:

type 'a ref { Contents : 'a } 
let (!) (x : ref 'a) = x.Contents 

由於!運營商期望bool ref,但您通過了bool,您會看到此錯誤。

使用not函數:

if not(File.Exists(doFile)) then 
    printfn "doFile doesn't exist %s" doFile; failwith "quit" 
7

in F#!不是不是,它是一個引用算子,這麼說不是,你需要使用沒有的功能,像if not <| File.Exists....

+2

,其中使用向後管的一個很好的例子,使事情變得更具有可讀性。 – Benjol 2011-06-01 05:56:21