codingXeno codingXeno - 9 months ago 62
Scala Question

How can I make a private member object-encapsulated?

In Scala, I know that by typing private[this] we can make a private member only accessible by that particular object of the class, and another object of the same class cannot access it. If possible, how can we accomplish this in other languages... particularly F#, Erlang, and Clojure? Thanks.

Answer Source

Please refer to @JohnPalmer's comment and also Access Control. Which can be easily found with Google.

type  TestPrivate(x)  =
    let y = x - 10
    member private __.X() = 10
    member __.Z() = x + 10
    member __.Y = __.X()

let test = TestPrivate(5)
test.Z() //val it : int = 15
test.Y //val it : int = 10

error FS0491: The member or object constructor 'X' is not accessible. Private members may only be accessed from wit hin the declaring type. Protected members may only be accessed from an extending type and cannot be accessed from inner lambda express ions.