9

Any ideas why the following doesn’t compile?

On the last line it tells me that Module1 is not defined. If I remove the “internal” from Module1 it works fine.

I've got two code files and Module1.fs is above Module2.fs in the project.

Module1.fs

module internal Module1

let sample =
    5 + 4

Module2.fs

module Module2

let sample2 =
    3 + Module1.sample

3 Answers 3

8

You'll need to give your modules a namespace so the internal module is visible to later ones.

let module internal MyNamespace.Module1
let module MyNamespace.Module2
2
  • 4
    For larger projects, I'd recommend omitting accessibility modifiers until the end, then adding signature files to take care of it.
    – Daniel
    Sep 6, 2011 at 16:07
  • 1
    It feels weird to me. Why we need another namespace to make internal functional?
    – colinfang
    Jul 27, 2013 at 20:28
5

Compiler bug

While the answers here are workarounds, this behavior is still a compiler bug. Reading the docs and Don Syme's Expert F#, there is no point that says that types in internal modules will be accessible only if you also use namespaces.

Considering the code the compiler emits I would not see a difficulty to make types inside internal modules visible inside the assembly.

Edit: Having filed this behavior to @fsbugs, the master himself, Don Syme, soon confirmed this being a bug. I added a workitem for this case:

https://visualfsharp.codeplex.com/workitem/29

1
  • If you think this is a bug have you submitted a bug report either to [email protected] or visualfsharp.codeplex.com? Apr 5, 2014 at 10:07
2

this should be a namespace problem. Just add namespace definitions on top of both your files (the same namespace!) like this:

namespace MyNamespace

module internal Module1 =

let sample = 5+4

and

namespace MyNamespace

module Module2 =

let sample2 = 3 + Module1.sample
7
  • This doesn't compile as the module is then a local module, but the idea was right - Daniel's solution works. Sep 6, 2011 at 15:59
  • NO - this did not compile because I forgot the "=" (as happens to me from time to time) - the solution is not very different from Daniels at all .... to be sure I just tested this code line to line ... and it surely is not some "local" problem - it works as expected. Of course you should insert some meaningfull namespace in there
    – Random Dev
    Sep 6, 2011 at 16:05
  • The "=" sign is required as the module definition inside the namespace is a local module, which I don't particularly want to use - this is not a "local" problem but an F# language distinction, see msdn.microsoft.com/en-us/library/dd233221.aspx Sep 6, 2011 at 16:09
  • ok - your choice - may I ask why? The indentation? As I read your linked article it's allmost useless: "If you have multiple files in a project or in a single compilation, or if you are building a library, you must include a namespace declaration or module declaration at the top of the file"
    – Random Dev
    Sep 6, 2011 at 16:14
  • I don't think it's a big deal either way, but it just seems simpler to use the top-level module declaration. And yes, indenting the entire module would be a bit annoying. Anyway, thanks for your help. Sep 6, 2011 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.