开发者

File checking code for F#

开发者 https://www.devze.com 2023-03-09 18:18 出处:网络
I have this code to raise an error when file doesn\'t exist. if !File.Exists(doFile) then printfn \"doFile doesn\'t exist %s\" doFile; failwith \"quit\"

I have this code to raise an error when file doesn't exist.

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

However, I got this error. What's wrong?

error FS0001: This expression was expected to have type
    bool ref    
but开发者_运维问答 here has type
    bool


The ! operator has a special meaning in F#, its defined as:

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

You're getting the error because the ! operator expects a bool ref, but you passed it a bool.

Use the not function instead:

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


in F# ! is not a NOT, it's a referencing operatior, so to say not, you need to use the not function, something like if not <| File.Exists....

0

精彩评论

暂无评论...
验证码 换一张
取 消