r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
403 stars 56 forks source link

FR: Support subclass to override superclass' cloning method #247

Closed IndrajeetPatil closed 1 year ago

IndrajeetPatil commented 2 years ago

Let's say I have a class called Creature. I wish to provide a $clone() method for this class, so I set cloneable = TRUE, and it works as expected:

library(R6)

# define a class
Creature <- R6Class(
  "Creature",
  cloneable = TRUE
)

# cloning for class objects possible
Creature$new()$clone()
#> <Creature>
#>   Public:
#>     clone: function (deep = FALSE)

Now, let's say I create a subclass Human that inherits from Creature. For this class, I don't want to provide $clone() method. So I set cloneable = FALSE, but that doesn't work:

# create a subclass
Human <- R6Class(
  "Human",
  inherit = Creature,
  cloneable = FALSE
)

# cloning still possible even when
# `cloneable` is set to `FALSE`
Human$new()$clone()
#> <Human>
#>   Inherits from: <Creature>
#>   Public:
#>     clone: function (deep = FALSE)

Created on 2022-03-01 by the reprex package (v2.0.1.9000)

Is it possible for {R6} to support this?