Is it possible to have private member fields inside of an R reference class. Playing with some of the online examples I have:
> Account <- setRefClass( "ref_Account"
> , fields = list(
> number = "character"
> , balance ="numeric")
> , methods = list(
> deposit <- function(amount) {
> if(amount < 0) {
> stop("deposits must be positive")
> }
> balance <<- balance + amount
> }
> , withdraw <- function(amount) {
> if(amount < 0) {
> stop("withdrawls must be positive")
> }
> balance <<- balance - amount
> }
> ) )
>
>
> tb <- Account$new(balance=50.75, number="baml-029873") tb$balance
> tb$balance <- 12
> tb$balance
I hate the fact I can update the balance directly. Perhaps that the old pure OO in me, I really would like to be able make the balance private, at least non-settable from outside the class.
Thoughts
To solve the issue of privacy I create an own class, "Private", which has new methods to access the object, i.e. $
and [[
. These methods will throw an error if the client tries to access 'private' member. Private member are identified by the name (leading period). As reference Objects are environments in R one can work around this, but it is my solution at this time and I think more convenient to use get/set methods provided by the class. So this is more the 'hard-to-set-from-outside-the-class' solution to the question.
I have organised this inside a R-package so the following code makes use of that package and modifies the above example such that an assignment to tb$.balance
produces an error. I also use the function Class
which is just a wrapper around setRefClass
so this is still in the scope of R's reference classes provided by the methods package and used in the question.
devtools::install_github("wahani/aoos")
library("aoos")
Account <- defineRefClass({
Class <- "Account"
contains <- "Private"
number <- "character"
.balance <- "numeric"
deposit <- function(amount) {
if(amount < 0) stop("deposits must be positive")
.balance <<- .balance + amount
}
withdraw <- function(amount) {
if(amount < 0) stop("withdrawls must be positive")
.balance <<- .balance - amount
}
})
tb <- Account(.balance = 50.75, number = "baml-029873")
tb$.balance # error
tb$.balance <- 12 # error
This answer doesn't work with R > 3.00, so don't use it!
As has been mentioned, you can't have private member fields. However, if you use the initialize method, then the balance isn't displayed as a field. For example,
Account = setRefClass("ref_Account",
fields = list(number = "character"),
methods = list(
initialize = function(balance, number) {
.self$number = number
.self$balance = balance
})
As before, we'll create an instance:
tb <- Account$new(balance=50.75, number="baml-0029873")
##No balance
tb
Reference class object of class "ref_Account"
Field "number":
[1] "baml-0029873"
As I mentioned, it isn't truly private, since you can still do:
R> tb$balance
[1] 50.75
R> tb$balance = 12
R> tb$balance
[1] 12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With