-
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
Should R7 offer encapsulated OO? #202
Comments
The discussion in #245 made me wonder if static methods/associated functions would be useful. That would allow "alternative constructors" etc. to live inside the class. No idea if this is feasible, but I was thinking like: range <- new_class("range",
properties = list(
start = class_numeric,
end = class_numeric
)
)
range@from <- new_generic("range@from", "x")
method(range@from, class_numeric) <- function(x) {
range(min(x), max(x))
}
range@from(1:5) == range(1L, 5L) I notice something like this has been considered based on the note about "class methods that lack self" in #191. |
The above idea is interesting and probably deserves its own issue. Since classes are objects, we would probably want some other syntax than |
It turns out to be trivial (#191) to add encapsulated methods to R7 (i.e. methods that belong to the class/object):
The current trivial implementation:
Handles within package inheritance of methods. Cross-package inheritance needs a little more work to ensure that we don't take a dependency at build-time, rather than load-time (so that environments of the methods match the currently installed package). Would need to adjust
super()
implementation to support method access.Doesn't have particularly good performance. Prformance will improve a little with a base C implementation of
@
, but would require byte code compiler transformation for high speed.Currently mutability "just works" if you inherit from environment:
But a full implementation would need careful consideration of how
validate()
should work to ensure that objects aren't left in an invalid state.Overall, I'm pretty neutral on this proposal. There are some big upsides, but also some big downsides. Here are a few of the pros/cons that I've considered so far:
x@up()
is actually modifyingx
in place: this is potentially extremely confusing..
would be sufficient? Since they wouldn't be documented and wouldn't be suggested by auto-complete, it would be unlikely for uses to accidentally use them.The text was updated successfully, but these errors were encountered: