openlang

F# negation when testing file existence

I'm using this code to signal an error if a file is missing.

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

Instead, this error appears. What did I misunderstand?

  error FS0001: This expression was expected to have type
      bool ref    
  but here has type
      bool

The conversation continues 2

Follow the reasoning
  1. Permalink

    in F#, ! doesn't mean NOT, it's the reference opperator, so negation requires the not function instead, for example you could write if not <| File.Exists....

  2. Permalink

    F# gives ! a different meaning, and thats defined as follows:

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

    This fails because you supplied a bool, whereas the operand required by ! is a bool ref.

    Replace it with the not function:

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

Other sides of the same question

  1. `String::from_utf8` gives the bytes back when it fails

    A consuming API has to decide what happens to the value when it fails. String::from_utf8 takes the Vec<u8> by value, and on f…

    Replies 0Latest activity rust
  2. except ValueError never runs around gather(return_exceptions=True), then TypeError on the ValueError

    I run a small script before client check-ins that scores each client's site from three metrics, with per-client weights fetch…

    Replies 5Latest activity python
  3. An errdefer free leaked on the success path

    A test leaked memory, and the leak was on the path that succeeded. The function allocated a scratch buffer, parsed into a res…

    Replies 2Latest activity zig