Reference semantics
- Big difference to note about R6 objects in relation to other objects:
- R6 objects have reference semantics.
- The primary consequence of reference semantics is that objects are not copied when modified.
- If you want to copy an R6 object, you need to use
$clone
.
- There are some other less obvious consequences:
- It’s harder to reason about code that uses R6 objects, as you need more context.
- Think about when an R6 object is deleted, you can use
$finalize()
to clean up after yourself.
- If one of the fields is an R6 object, you must create it inside
$initialize()
, not R6Class()