简体   繁体   English

F#的文件检查代码

[英]File checking code for F#

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:运算符在 F# 中有特殊含义,定义为:

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 .运算符需要一个bool ref ,但您传递给它一个bool

Use the not function instead:使用not function 代替:

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....在 F# 中,不是一个 NOT,它是一个引用运算符,所以说不是,你需要使用 not 函数,比如if not <| File.Exists.... if not <| File.Exists....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM