当文件不存在时,我有这段代码会引发错误。

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

最佳答案

!运算符在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"

关于F#的文件检查代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6189526/

10-12 18:06