Strange accessibility areas when F # record fields are declared private

I noticed a rather counter intuitive behavior when the field part of the F # record is declared closed. (This is due to Is it possible to make the recording field private? Or to make the recording of the recording private? )

In this example ...

type MyRec = private // Fields declared private, or at least I thought so. { a : int b : int } member xA = xa member private x.Both = xa + xb static member CreateMyRec(a, b) = { a = a; b = b } let foo = MyRec.CreateMyRec(1,2) let bar = foo.a // No error. Huh? let baz = foo.Both // Error: not accessible. 

... Both private member is not available outside the scope of the type declaration as expected. However, field a available.

If you put MyRec in a module, the fields become private for that module. The way you expect the top-level declaration in a module to behave, but I expected that everything declared private in a type would be closed to this type, and not to its closing module.

Is this behavior really strange, or am I missing something in my reasoning here?

+6
source share
1 answer

As far as I can tell, this is an underrated feature. But, in section 10.5 of the specification, Availability Annotations , it says:

private in the representation of a type, module, or type in a module means "private to the module."

"type representation" is part related to the fields of a record.

+10
source

All Articles