F # Function / friend class

Is it possible to implement a friend function and a friend class (as in C ++) in F #?

Update : Since f # does not have a friend function / class, and the friend does not even have a reserved keyword for a future extension, I am wondering if there are any problems with the friend mechanism in F # that force developers to decide not to execute it? (for example, in a “secure” access modifier ).

Proposition 1 : Brian, signature file - I do not think this works correctly. If you have a closure (e.g. lambda expression in A, which is a different object than instance A) that evaluates to BX, it will not work

Proposition 2 : Massif (+ Mitya0), InternalsVisibleTo - It’s not clear to me whether you write this in the second class or expose the class for the whole assembly?

+3
source share
4 answers

Please note that you can use signature files to emulate a friend.

If you want to Abe a friend B, so you Acan access internal elements Bthat others cannot see, you can do, for example,

// 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 the Aimplementation can see B.X, but the sequel to the program cannot see B.X.

Signature files are pretty impressive for creating arbitrary encapsulation boundaries.

+11
source

. InternalsVisibleTo , , . ( ).

[<assembly:System.Runtime.CompilerServices.InternalsVisibleTo("UnitTestModule")>]

.

+3

, ++, F #, internal, .NET. , , .

+2

, , #,, , F # .

+1

Source: https://habr.com/ru/post/1753646/


All Articles