0

suppose I have a Reference class definition in R which has fields which itself are reference classes:

A <- setRefClass("A", fields = list(aVar = "numeric"))

B <- setRefClass("B", fields = list(bVarOne = "numeric", bVarTwo = "ANY"))

myBinst <- B$new(bVarOne = 10, bVarTwo = A$new(aVar = 5))

str(myBinst)
Reference class 'B' [package ".GlobalEnv"] with 2 fields
 $ bVarOne: num 10
 $ bVarTwo:Reference class 'A' [package ".GlobalEnv"] with 1 field
  ..$ aVar: num 5
  ..and 12 methods.
 and 12 methods.

Reference class instances can refer to themselves with .self, could they refer to an instance of a reference class that contains them as a field i.e. in this example, can the instance of A contained within the instance of B refer to B? If so, how can I do this?

Thanks, Ben.

SJWard
  • 3,629
  • 5
  • 39
  • 54
  • Are you trying to model a particular process or structure? – IRTFM Jul 18 '15 at 19:45
  • It sounds like you are trying to build a linked list or tree structure. You'll probably have to explicitly specify relations between things. – Spacedman Jul 18 '15 at 21:44
  • Reference classes allow (multiple) inheritance. Perhaps you could find a smarter solution, when B is a sublass of A? (`contains = ...`). Parent pointers in A could be also a approach, but cause a bunch of problems when copying objects. – Patrick Roocks Aug 03 '15 at 09:24

0 Answers0