Private modifier for types F #

If we have

module M type private A = B | C (...) 

A private modifier means that A will only be used from module M. But in this case:

 namespace N type private A = B | C (...) 

What will be the effect of this private modifier? Doesn't it make sense for the CLR namespace to have private members, since the namespace can be expanded as desired, so this private modifier can have any effect?

+6
source share
1 answer

If you use the private modifier, similar to what is in the namespace, it makes the type or module closed to the file - that is, the type can be used by something under it under the same file.

When a type is compiled, the F # compiler will treat it as internal in the .NET metadata. (Which makes sense since the type is really internal - the compiler simply enforces the restriction that it can only be used by types / functions inside the same file.)

I use this function quite often BTW - it is good for small helper types that are used only internally in only a few places, as this prevents them from cluttering IntelliSense.

EDIT: Another cool trick you can use for this is to create built-in helper functions with file capabilities.

If you create such a module, the functions in it will be automatically available for anything below (thanks [<AutoOpen>] ), but since the module is marked private , the functions will not clog your IntelliSense (when working with your other source files) or hide existing ones functions.

 [<AutoOpen>] module private Helpers = let [<Literal>] blah = "blah" let lessThanTwo value = value < 2.0 
+6
source

All Articles