2

I'm trying to create a class in R called move, and want one of the fields to be type move as well. I know this is possible in Java, but I'm unsure how to do this in R (if it can be done).

I've tried searching for examples, but have been unable to find any. This is what my code looks like:

move = setRefClass("move", fields=list(pos="numeric", backtracker="move"))

This is the error I get when trying to run the above line:

Error in refClassInformation(Class, contains, fields, methods, where) : class “move” for field ‘backtracker’ is not defined

Is it possible to do something like this in R?

Thanks in advance!

Hayden Y.
  • 448
  • 2
  • 8
aklobo
  • 21
  • 3

1 Answers1

1

One way to do it would be to use ANY to avoid the "chicken and egg" problem e.g.

move = setRefClass("move", 
  fields = list(
    pos="numeric", 
    backtracker="ANY"
  )
)

mov1 <- move$new()
mov1$pos <- 1

mov2 <- move$new()
mov2$pos <- 2
mov2$backtracker <- mov1  

print(mov2$backtracker$pos)

[1] 1
Per
  • 340
  • 4
  • 9