Eben Kadile
Eben Kadile

Reputation: 779

How to import in F#

I have a file called Parser.fs with module Parser at the top of the file. It compiles. I have another module in the same directory, Main, that looks like this:

module Main

open Parser

let _ = //do stuff

I tried to compile Main.fs with $ fsharpc Main.fs (idk if there's another way to compile). The first error is module or namespace 'Parser' is not defined, all other errors are because of the fact that the functions in Parser are not in scope.

I don't know if it matters, but I did try compiling Main after Parser, and it still didn't work. What am I doing wrong?

Upvotes: 2

Views: 1597

Answers (1)

Fyodor Soikin
Fyodor Soikin

Reputation: 80915

F#, unlike Haskell, does not have separate compilation. Well, it does at the assembly level, but not at the module level. If you want both modules to be in the same assembly, you need to compile them together:

fsharpc Parser.fs Main.fs

Another difference from Haskell: order of compilation matters. If you reverse the files, it won't compile.

Alternatively, you could compile Parser into its own assembly:

fsharpc Parser.fs -o:Parser.dll

And then reference that assembly when compiling Main:

fsharpc Main.fs -r:Parser.dll

That said, I would recommend using an fsproj project file (analog of cabal file). Less headache, more control.

Upvotes: 4

Related Questions