F#的文件检查代码

pro*_*eek 11 f# file

我有这个代码在文件不存在时引发错误.

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

但是,我收到了这个错误.怎么了?

error FS0001: This expression was expected to have type
    bool ref    
but here has type
    bool
Run Code Online (Sandbox Code Playgroud)

Jul*_*iet 17

!运营商在F#有特殊的意义,它的定义为:

type 'a ref { Contents : 'a }
let (!) (x : ref 'a) = x.Contents
Run Code Online (Sandbox Code Playgroud)

你得到错误是因为!操作员期望a bool ref,但是你传递了它bool.

请改用此not功能:

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


Ale*_*lex 7

在F#中!不是NOT,它是一个引用操作符,所以要说不是,你需要使用not函数,比如if not <| File.Exists....

  • 使用向后管道的一个很好的例子使事情更具可读性. (2认同)