2
votes

Using Visual Studio 2015 Update 3 and fsi.exe from F# v4.0 I' trying to run this script:

//error.fsx

#if INTERACTIVE
    let msg = "Interactive"
#else
    let msg = "Not Interactive"
#endif

let add x y =
    x + y

printfn "%d" (add 1 2)

Output: error.fsx(12,15): error FS0039: The value or constructor 'add' is not defined

If I then comment out the #if-#else-#endif block, it works fine:

// fixed.fsx

//#if INTERACTIVE
//    let msg = "Interactive"
//#else
//    let msg = "Not Interactive"
//#endif

let add x y =
    x + y

printfn "%d" (add 1 2)

Output: 3

I'm sure I'm doing something wrong (rather than this being a bug) but I can't for the life of me figure out how to make this work.

Thoughts?

1

1 Answers

5
votes

This is caused by the indentation of let msg. The following works fine:

#if INTERACTIVE
let msg = "Interactive"
#else
let msg = "Not Interactive"
#endif

let add x y =
    x + y

printfn "%d" (add 1 2)

I do have to say, I'm not entirely sure why the code gives exactly the error message you mention - I suspect this is a bug in the error reporting and it wold be worth reporting it to the F# team. At the very least, there should be a sensible error message!

It seems that with the indentation, the F# parser actually parses the code as follows:

let msg = "Interactive"
let add x y =
  x + y 
    printfn "%d" (add 1 2)

The spacing before let msg causes the compiler to treat printfn as being indented too. You can see that this is the case if you look at the type of the y variable in your editor using a tool tip...