Is it possible to implement friend function and friend class (as in c++) in F#?
Update: Since there is no friend function/class in f#, and friend is not even a reserved keyword for future expansion, I'm wondering is there any problems with the friend mechanism in F# that make the developers to decide not to implement it? (such as in "protected" access modifier).
Suggestion 1: Brian, signature file - I don't think that this thing work properly. If you have a closure (e.g. lambda expression in A, which is a different object than the instance of A) that evaluate B.X , it's won't work
Suggestion 2: Massif (+Mitya0), InternalsVisibleTo - It's not clear to me, Are you writing this in the second class or it expose the class to the entire assembly?
Note that you can use signature files to mimic friend.
If you want A
to be a friend of B
, so A
can access internals of B
that others cannot see, you can do e.g.
// File1.fs
type B() =
let x = 42 // private field
member this.X = x // public getter
type A() =
member this.PeekInto(b : B) =
b.X
but also have
// File1.fsi
type B =
new : unit -> B
// do not expose X in the signature
type A =
new : unit -> A
PeekInto : B -> int
and now A
's implementation can see B.X
but the sequel of the program cannot see B.X
.
Signature files are pretty awesome for creating arbitrary encapsulation boundaries.