The equivalent to import
is open
.
If I take your pastebin code, it has a bunch of errors indeed, as shown on SharpLab. There are a few things to note here:
- F# likes its code ordered. Each file is considered a separate compilation unit and anything that you need from it can only be referenced in a following file, not in a previous one.
- Inside a single file, you can create circular references with
and
, but otherwise, the same applies: whatever type, value, module you want to use, it must already exist and be in scope.
- You created a log-value, not a log-function.
- You forgot the
=
sign after the module
definitions.
Your original code is this:
// Program.fs
module main
open Log
[<EntryPoint>]
let main argv =
printfn "Hello"
log
0 // return an integer exit code
// Log.fs
module Log
let log =
printfn "Hello"
This gives the following errors:
error FS0039: The namespace or module 'Log' is not defined.
You get this one because you have a open Log
, but the module Log
doesn't exist yet.
error FS0010: Unexpected start of structured construct in definition. Expected '=' or other token.
This is about the last let
, it must be indented.
error FS0010: Incomplete structured construct at or before this point in implementation file
Same thing, caused by the previous error.
After I change the order of your code, indent it appropriately, change let log
to let log()
, and add the necessary =
signs, it just works, try it out:
// Log.fs
module Log =
let log() =
printfn "Hello"
// Program.fs
module main =
open Log
[<EntryPoint>]
let main argv =
printfn "Hello"
log()
0 // return an integer exit code
Please note that inside a module you can remove the first level of indentation and the =
-sign, but only if it is the only module in that file and it is the last file in your project (so generally I would advise against that, to keep it simple, just always indent and always have the =
-sign there).
But just to show you an alternative that also works:
// Log.fs
module Log =
let log() =
printfn "Hello"
open Log
[<EntryPoint>]
let main argv = // it is the last in the file or prj, this is allowed
printfn "Hello"
log()
0 // return an integer exit code
Note also that if you place the code in different files, that you must add a namespace declaration at the very top of the file. Typically this will be the default namespace of your whole project.