RConsortium / S7

S7: a new OO system for R
https://rconsortium.github.io/S7
Other
386 stars 32 forks source link

infinite recursion when trying to access an uninitialized property with getter function #403

Open rikivillalba opened 2 months ago

rikivillalba commented 2 months ago

When trying to access an uninitialized property that has a getter, a stack overflow occurs


library(S7)

someclass <- new_class("someclass", properties = list(
  action = new_property(
    class_character,
    getter = function(self) self@action,
    setter = function(self, value) {
      self@action <- toupper(value)
      self 
    },
    default = "some action"
  )
))

x <- someclass()
x@action

#> Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

as soon as I set a value the code works. Is this behaviour ok? default property value also does not help.

hadley commented 2 months ago

This seems like a bug to me.

t-kalinowski commented 2 months ago

One solution might be to stop filtering out dynamic properties from the constructor args. I.e., remove this line (and update tests to match)

https://github.com/RConsortium/S7/blob/ffb8ca92abeb6a51f0a3ecad88d46ad29e18a7ae/R/constructor.R#L51

The object constructor would then have signature function(action = "some action").

This intersects with https://github.com/RConsortium/S7/issues/376, where we are also discussing changing the object constructor signature.