Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rikivillalba opened this issue May 27, 2024 · 3 comments · Fixed by #406
Closed

Comments

@rikivillalba
Copy link

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
Copy link
Member

hadley commented May 27, 2024

This seems like a bug to me.

@t-kalinowski
Copy link
Member

t-kalinowski commented May 28, 2024

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

self_args <- self_args[vlapply(properties, function(x) is.null(x$getter))]

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

This intersects with #376, where we are also discussing changing the object constructor signature.

@lawremi
Copy link
Collaborator

lawremi commented Aug 26, 2024

The current design of property getting is confusing to me. It seems to require that a property with a getter not use its underlying attribute value at all. The getter is run if and only if the attribute value is NULL.

I would prefer that instead we always run the getter, if present, and we use the same trick as property setting to avoid infinite recursion (as already proposed in the C code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants