How to open modules in F# Interactive?

Been learning F# for a while, I do like it. In Visual Studio 2017, for some reason on some .fs files I can open a module and it will work just fine in the editor. When I try to run scripts in F# interactive that rely on opening another module I get a ‘namespace or module is not defined error’. For some reason, it does not give me the same error when I open System. I’ve made sure to organizing it so that the required module to open comes before the script that wants to call it. It will work when I’m coding in the editor but breaks when I try to execute in interactive. Thanks in advance.

I’m quite rarely use F# Interactive but never seen such behaviour.
I think it should work the same as in editor.

If you don’t get error when trying to get access to members of System namespace that probably means you already opened it somewhere.

If you can reproduce the issue, please share minimal sample of code. Then it will be much easier to understand where problem lays.

Just a note: F# has AutoOpen attribute.

Looks like it may have been a noob mistake. I had to type in – #load into FSI for it to actually load the modules. Don’t have to do that in a normal .fs file, only in .fsx. This may just be normal.

From your description, it sounds like the module you want to use is not in your script, but in another file, either an .fs or .fsx file, is that correct? Something like MyFile.fs, with

module MyModule = 
    let hello (name: string) = sprintf "Hello, %s" name

Script files behave a bit differently from .fs files in a solution: they don’t belong to the solution, and are completely independent. If you want to use a resource that is in a different file, you have to explicitly import it. In this case, to use the hello function in a script, first you would #load "path/to/MyFile.fs, making the whole file contents available in your scripting environment, and then either open MyModule, or call MyModule.hello "world".
If you wanted to use a dll, you would similarly use #r path/to/library.dll.
Note also that file order doesn’t matter for scripts, but load order might.
Hope this helps :slight_smile:

1 Like

Wow thanks for the detailed info! It makes sense, no problem now! I’m a C# developer and have fallen in love with F# and functional programming in general, it’s exciting to see a lively community behind the language.

I would like to add that according to cartermp

In the future, you’ll be able to #r "package.name"

I dont know if we are there yet, I have been away from Fsharp for a while now
but as per Mathias’ answer, it seems we are not there yet

#r “package.name”

without having to point to a path, is huge in my opinion

1 Like

What about transitive dependencies between different scripts/modules/files in interactive ?
how does that work? F# interactive fails to load proper project "version" when using multiple scripts load · Issue #13866 · dotnet/fsharp · GitHub.