3

I am trying to "pass" a reference to an Reference Class object (say, a ball) between two other Reference Class objects (say, two soccer [football] players) with the following example:

# create Reference classes
b <- setRefClass('Ball', fields = list(size = 'numeric'))

    p <- setRefClass('Player', fields = list(name = 'character', possession = 'Ball'), 
methods = list(pass = function(){
        tmp <- possession$copy()
        possession <<- NULL
        return(tmp)
    }, receive = function(newBall){
        possession <<- newBall
} 
))


# initialize pretend all-star team
# p1 gets initial possession of a new ball
p1 <- p$new(name = 'Ronaldinho', possession = b$new(size=5) )

p2 <- p$new(name = 'Beckham')

# now pass the ball from p1 to p2
p2$receive(p1$pass())

However I get the following error:

Error in function (value)  : 
  invalid replacement for field ‘possession’, should be from class “Ball” or a subclass (was class “NULL”)

Theoretically I am trying to retutn a reference to the ball object, and then add that reference to the other player, but obviously that is not working. I know it is possible to achieve the same results by accessing the fields directly, but I would like to find a way to accomplish this "pass" using internal methods of the class only. Is this possible? Why am I getting this error?

csgillespie
  • 59,189
  • 14
  • 150
  • 185
Gary Weissman
  • 3,557
  • 1
  • 18
  • 23
  • Please edit your question. None of the authors of either S3, S4 or Reference Classes like the term you used here for the latter. The term has propagated enough, let's stop it. They are called "Reference Classes". – Dirk Eddelbuettel Feb 23 '13 at 23:46
  • I wasn't aware there is a debate around the name of these classes. I derived the term from the very helpful introduction at: [hadley's R5 wiki](https://github.com/hadley/devtools/wiki/R5). Happy to change it if that is not the correct term. – Gary Weissman Feb 23 '13 at 23:48
  • I have been leaning *hard* on Hadley to not further propagate the term. He is starting to come around. – Dirk Eddelbuettel Feb 23 '13 at 23:49
  • what is the confusion/debate surrounding the class naming? If there were another easily google-able and straightoforward introduction elsewhere on 'Reference Classes' it would probably encourage others using the alternative name. – Gary Weissman Feb 23 '13 at 23:50
  • I edited the devtools wiki. As for why, see my first answer. And a heartfelt 'Thank you' for editing your question. – Dirk Eddelbuettel Feb 24 '13 at 00:01
  • Of course. It makes sense to have consistency in naming conventions, and good for me know when I'm googling for help in the future! – Gary Weissman Feb 24 '13 at 00:08

1 Answers1

1

You can get an error because when you define your Player class, you set the possession field to have type Ball. But in your pass function, you set possession to be NULL:

possession <<- NULL

If you change the initialisation to:

p = setRefClass('Player', fields = list(name = 'character', possession = 'ANY')

then everything works as expect:

R> p2$receive(p1$pass())
R> p1
Reference class object of class "Player"
Field "name":
[1] "Ronaldinho"
Field "possession":
NULL
R> p2
Reference class object of class "Player"
Field "name":
[1] "Beckham"
Field "possession":
Reference class object of class "Ball"
Field "size":
[1] 5
csgillespie
  • 59,189
  • 14
  • 150
  • 185