F# Friend function/class
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 t开发者_开发问答he 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.
As mitya has pointed out. Just use the InternalsVisibleTo attribute on the class you want to see the protected members of, or on the entire assembly. (I do this all the time for unit testing purposes).
[<assembly:System.Runtime.CompilerServices.InternalsVisibleTo("UnitTestModule")>]
Works a treat.
The concept of friends as it exists in C++ doesn't exactly translate to F#, but you can use the internal
accessibility modifier to allow access to all classes in the same .NET assembly. This is probably what you are looking for.
From what I see of documentation, there is a friend notion in C#, but the same documentation suggests F# doesn't have this ability.
精彩评论