Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File checking code for F#

Tags:

file

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
like image 515
prosseek Avatar asked May 31 '11 14:05

prosseek


2 Answers

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"
like image 114
Juliet Avatar answered Oct 01 '22 12:10

Juliet


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....

like image 21
Alex Avatar answered Oct 01 '22 12:10

Alex