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

Fix getter infinite recursion #406

Merged
merged 11 commits into from
Sep 6, 2024
Merged

Fix getter infinite recursion #406

merged 11 commits into from
Sep 6, 2024

Conversation

t-kalinowski
Copy link
Member

Closes #403

R/constructor.R Outdated
@@ -48,7 +48,7 @@ constructor_args <- function(parent, properties = list()) {

self_args <- names2(properties)
# Remove dynamic arguments
self_args <- self_args[vlapply(properties, function(x) is.null(x$getter))]
self_args <- self_args[vlapply(properties, function(x) is.null(x$getter) || !is.null(x$default))]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that a property with only a getter could be computed entirely dynamically, so we wouldn't want it in the constructor. I still think we should use the prototype approach, where the object just starts with its defaults, and there's no need to set a read-only property during construction.

Btw, there is actually a bug here, where a property with a setter (and a getter) is dropped, in error. This is addressed by the prop_is_read_only branch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should the interface be for someone looking to implement a class with a property that can be set when the instance is initially constructed but is read-only after that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, this looks good to me: main...prop_is_read_only

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See issue #404 for my proposed approach to construct-only properties.

@lawremi
Copy link
Collaborator

lawremi commented Aug 26, 2024

Are you sure we can use the same list for both getting and setting? Assume a setter is called and it wants to get the value of the property, I think it would be surprising that the getter is not called.

As an example of where this would arise, assume a property is fully dynamic, like the value is being stored in a database, or on a C data structure. Maybe the setter wants to make sure that the value is different before performing the change.

I think we can still share a lot of the code between setting and getting, even if we use two different lists (the list symbol can just become an argument to the functions that manipulate the list).

@t-kalinowski
Copy link
Member Author

Are you sure we can use the same list for both getting and setting?

My thought process was that in property getter/setter methods, we're in "internal" territory, where accessing the property fetches the underlying attribute. However, you make a compelling case that it would be useful to be able to call a getter() from a setter() method.

I'll change the approach to allow for that.

@t-kalinowski t-kalinowski marked this pull request as ready for review September 6, 2024 17:50
@t-kalinowski t-kalinowski merged commit d85c904 into main Sep 6, 2024
11 checks passed
@t-kalinowski t-kalinowski deleted the fix-getter-recursion branch September 6, 2024 19:20
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 this pull request may close these issues.

infinite recursion when trying to access an uninitialized property with getter function
2 participants