views:

184

answers:

1

I've recently been trying to learn the Object-Oriented aspects of F#, and have become curious about how to restrict access to types/modules in the language.

More specifically, I want to know the difference between writing this:

Example.fsi

module Stack =
    val foo : string

Example.fs

module Stack =
    let foo = "foo"
    let bar = "bar"

and alternatively this:

module Stack =
    let foo = "foo"
    let private bar = "bar"

Do they not accomplish exactly the same thing in the end? Coming from a C# background, I'm much inclined just to use the access modifiers over signature (FSI) files. They seem to be more versatile (can apply to modules/types in namespaces, for example), whereas I don't any situation in which signature files offer something that access modifiers don't.

+4  A: 

They accomplish almost the same thing. (Note that you can use an .fsi file for types in namespaces too, was unsure what your comment about that meant.)

A signature file has a couple advantages:

  • You can make entities public for the duration of the file, but then private to the subsequent files of the project.
  • You can have just your short summary in the signature file, so the public interface is easy to read without having to scan tons of code.

The first bullet is not to be trifled with - within-assembly encapsulation like this is actually a pretty huge feature for very large projects. Being able to define a few types which are public to one another within File1.fs, but then only have a subset of those types/methods be public to the rest (File2.fs, File3.fs, etc.) is quite useful (a little bit like 'friend' in C++).

Brian
@Brian: Thanks for the answer. I was referring to the bottom of this page - http://en.wikibooks.org/wiki/F_Sharp_Programming/Modules_and_Namespaces - not being able to use FSIs on namespaces. Could you clarify what you mean by "the sequel" of the project?
Noldorin
I see, that document is wrong; @Juliet, if you are reading this, would you update it? Note that you can use fsc --sig on a .fs file containing namespaces to see the syntax for .fsi code for namespaces.
Brian
@Brian: Ah, fair enough then. Useful little command, that is. Thanks for the clarification, too.
Noldorin