Repository navigation
Description
Sometimes it's useful to be able to nest objects of the same type inside each other. Is there a recommended way for doing this with R7?
Naive approach
It's good that this errors; if you ever tried to instantiate this you'd end up in an infinitely recursing loop.
nested <- new_class("nested",
properties = list(child = nested)
)
#> Error in as_properties(properties): object 'nested' not foundCreated on 2022-08-14 by the reprex package (v2.0.1)
new_union()
It would be nice if this worked, allowing you to specify at least one other type that can be a "base case". Preferably while still protecting you from infinite recursion by not permitting the nested class to be the default.
nested <- new_class("nested",
properties = list( child = new_union(NULL, nested) )
)
#> Error in lapply(x, as_class): object 'nested' not foundCreated on 2022-08-14 by the reprex package (v2.0.1)
Define it twice
This doesn't error and lets me nest objects, but properties holds on to the original class definition (inner's child has no name or child properties, since the original definition of nested had no properties). I don't know what problems that might cause (and it prevents the infinite recursion problem), but I don't like the feel of it.
nested <- new_class("nested")
nested <- new_class("nested",
properties = list(
name = class_character,
child = nested )
)
inner <- nested()
inner
#> <nested>
#> @ name : chr(0)
#> @ child: <nested>
outer <- nested(name = "outer", child = inner)
outer
#> <nested>
#> @ name : chr "outer"
#> @ child: <nested>
#> .. @ name : chr(0)
#> .. @ child: <nested>Created on 2022-08-14 by the reprex package (v2.0.1)
Use a parent class
This seems to work and I haven't thougth of anything that would go wrong. It's mildly annoying to have the extra parent class cluttering things up if I don't have any other reason for it to exist, but I can live with it :)
nested_parent <- new_class("nested_parent")
nested <- new_class("nested", parent = nested_parent,
properties = list(
name = class_character,
child = nested_parent )
)
inner <- nested()
inner
#> <nested>
#> @ name : chr(0)
#> @ child: <nested_parent>
outer <- nested(name = "outer", child = inner)
outer
#> <nested>
#> @ name : chr "outer"
#> @ child: <nested>
#> .. @ name : chr(0)
#> .. @ child: <nested_parent>Created on 2022-08-14 by the reprex package (v2.0.1)
So, 2 requests:
- It'd be nice to have documentation on the recommended way to create nestable classes. Is using a parent class the way to go?
- Any chance some behind-the-scenes wizardry could make something like the
new_unionsyntax work?
Activity
hadley commentedon Aug 15, 2022
Hmmmm, we haven't thought about this problem yet. Do you have any examples of syntax from other OO systems?
lawremi commentedon Aug 15, 2022
We could probably reuse the delayed binding stuff that enables optional dependencies. That does require some effort on the part of the developer but it is pretty clean. S4 got away with this by just using strings.
hadley commentedon Aug 15, 2022
@lawremi we currently only have
new_external_generic(), but we could addnew_external_class()too.lawremi commentedon Aug 15, 2022
I think it would be useful for defining a method on a local generic but with an external class in the signature. Like if a package defines a common API.
KatherineCox commentedon Aug 27, 2022
As I've been experimenting a bit more, I realized this is more general - I can only use one R7 class as a prop type for another if the inner one is defined first.
This feels surprising because it's not how functions work; I'm used to being able to organize my code however I want.
mmaechler commentedon Aug 29, 2022
Well, it's exactly like this with the only "strict" class system in (base) R:
S4.In the
{Matrix}package (and also in{Rmpfr}and{copula}if I remember correctly), we have usedAllClass.RandAllGeneric.R(and did not use other file names alphabetically before"All*") and then really put all (basic) class definitions in a consistent order intoAllClass.R.Of course the alternative is to use explicit collation in the
DESCRIPTIONfile, something I think is advocated by the Roxygen aficionados. I personally hate to add (and update, even if automatically) such aCollate:field in DESCRIPTION.jl5000 commentedon Feb 14, 2023
I have a related problem of trying to define two classes, each dependent on the other. I'm not sure whether resolving the collate issue above will actually make this recursion possible, but I have a problem that requires it.
Created on 2023-02-14 with reprex v2.0.2
hadley commentedon Sep 10, 2023
I just realised that there's one easy way to solve this problem by taking advantage of the fact that S7 classes are S3 classes:
Created on 2023-09-10 with reprex v2.0.2
I can't quite decide if this is beautiful or horrible, but it is a simple and pragmatic way to solve the problem today. And if we do introduce a solution sometime in the future, the underlying implementation is likely to be quite similar, but will just require some small tweaking to get a better error message, i.e.:
(Note that in both the examples above I think you have to make the values "optional" because otherwise I don't understand how you'd ever end the chain. Using (e.g.)
NULL | class_oneis slightly better thanclass_one | NULLbecause it means we don't need to supply an explicit default as the default default value is taken from the first element of a union.)jl5000 commentedon Sep 10, 2023
This works, until you decide to use the
packageparameter innew_class().hadley commentedon Sep 11, 2023
@jl5000 then you can use
new_S3_class("pkg::class_one")lawremi commentedon Aug 9, 2024
After looking at this again, there are two potential solutions:
I think the stub approach is best, since supporting references would be too much at odds with the current approach. Two examples of stubs are the "define the class twice" approach at the top of this issue and Hadley's S3 approach. I think I prefer the former, because at least then it's a proper S7 class. Either way, we should document a recommended approach.
ltuijnder commentedon Jan 21, 2025
While the stub approach works great. I do see the following drawbacks:
@. However, this is really only possible if my editor has the correct class definition. Stubs break this.All these issues are "nice to haves". I am already happy that it works :). But it would be great if we could somehow help static code tools to point towards the real class definition.
Implement external classes (#660)