简体   繁体   中英

What is the equivalent of “import” in F#

How can I import a function from another file in F#? Like you do import in Python. I tried open , #using , nothing works. I looked on the official docs and I still can't figure out how to use it.

Basically, I want something like this:

// Log.fs

module Log = 
    let log = 
        printfn "Hello, World"
// Program.fs
open Log

module main = 
    [<EntryPoint>]
    let main argv =
        log // Not working
        0

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:

  1. 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.
  2. 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.
  3. You created a log-value, not a log-function.
  4. 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.

This works.I get an error (in VS 2017) when using your syntax Files in libraries or multiple-file applications must begin with a namespace or module declaration. When using a module declaration at the start of a file the '=' sign is not allowed. If this is a top-level module, consider removing the = to resolve this error. Files in libraries or multiple-file applications must begin with a namespace or module declaration. When using a module declaration at the start of a file the '=' sign is not allowed. If this is a top-level module, consider removing the = to resolve this error.

// Log.fs

module Log

let log = 
    printfn "Hello, World"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM