-
Notifications
You must be signed in to change notification settings - Fork 37
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
Getter and Setter are not called anymore once they crash. #520
Comments
Ok playing around a bit more. The root cause seems that once an error is thrown in the getter. The getter is not called anymore and the underlying value is returned. Even if the object went back to a valid state. This seems not like desired behaviour because even if the end-user handled the error gracefully they are still left with an broken object where the getter does not work anymore. Consider the toy example below where the value stored internally is 1 higher. library(S7)
Test <- new_class(
"Test",
properties = list(
a = new_property(
class = class_numeric,
setter = \(self, value) {
print("setting")
self@a <- value + 1
self
},
getter = \(self) {
print("getting")
if (self@a == 10) stop("self@a = 10")
return(self@a - 1)
}
)
)
)
t <- Test(a = 1)
# [1] "setting"
t@a
# [1] "getting"
# [1] 1
t@a
# [1] "getting"
# [1] 1
t@a <- 9
# [1] "setting"
t@a
# [1] "getting"
# Error in (function (self) : self@a = 10
t@a
# [1] 10
t@a <- 1
# [1] "setting"
t@a # Getting is not called anymore.
# [1] 2 |
Thank you very much for the bug report. This is happening because the attribute I think we'll need to install a calling handler here, to allow for clearing the That should also help with giving more informative tracebacks on errors from custom getters. |
I would like to implement an "initialization"-pattern with a S7 class. That is certain properties can only be accessed once the object is flagged as initialized (eg. all necessary resources have been allocated).
In the getter of the property I thus check if the object is initialized and raise an error if this is not the case. However, the getter seems to only run once and then afterwards the check is simply by-passed. I expected the getter to be called everytime.
The property in question also needs to be settable as it represent a resource that got allocated on initialization and thus needs to be assignable.
The following code highlights my problem:
I know that the end-user could also perform a check
if(!t@init)
. But then the check in the getter becomes pointless in the first place. I would like the class to self contained and guard against wrong usage of uninitialized properties.The text was updated successfully, but these errors were encountered: