3
votes

I create a new F# console application project in Visual Studio 2015 I create a simple new module file called Calc.fs like so:

module Calc

let add a b = a + b

I add an F# script file to the project like so:

open Calc

let c = add 1 2 

I get the following compiler errors in the script file:

The namespace or module 'Calc' is not defined

and

The value or constructor 'add' is not defined

Why is my module not recognized by my script file? How can I fix this?

Please note that my script file appears after the module in the order of files in the project:

enter image description here

1
F# has strict order of compilation. You need to move Calc to a position before Program in the file list. - Fyodor Soikin
Thanks. I just tried that, moving Program.fs to the bottom. Still doesn't work. - urig

1 Answers

13
votes

.fsx files are not compiled together with the rest of the project; they don't "know" about any other code unless you explicitly make it known to them. This is true for any external DLLs (and in fact even many in the .NET Framework) as well as other F# code.

You need to add

#load "Calc.fs"

at the top of the script file to tell the compiler to load that code before evaluating the rest of the script.