Skip to content

Export S7_class_storage() for packages that rebuild objects from their attributes #806

Open

Description

@nbenn

Since S7 1.0.0, the _S7_class attribute of an ordinary object holds the class reference that the environment of its class's constructor binds as .S7_class_ref (#742). A package that rebuilds an object from its attributes, the way readRDS() does, has to put that reference back, and S7 exports no way to get it from a class. S7 does the lookup internally in S7_class_storage(), which returns the class itself for a class whose constructor binds no reference, as one an older S7 built.

The typedjson package writes R values as JSON and reads them back. It rebuilds an S7 object from its attributes without running the constructor, then calls validate(). Its support for S7 1.0.0 (nbenn/typedjson#120) reads three internals:

The covr package does the same lookup through the .S7_class_ref and class bindings, to find the shared class behind an installed class object, which can be a byte-compiled copy (r-lib/covr#646). For such a copy, S7_class_storage() returns the reference covr reads, and the class binding of that reference is the shared class.

This is the R-level counterpart of #788, which gave native code a stable way to read an object's class. A package that rebuilds an object, or reaches the class behind a copy of one, needs the other direction, from a class to what its objects store. Would exporting S7_class_storage(), with its return value documented, be worth considering? It would cover every use above but one, the environment that typedjson strips from a constructor. With export(S7_class_storage) added to the NAMESPACE of S7 1.0.0 and typedjson's lookup replaced by S7::S7_class_storage(), the typedjson tests pass as they do with the internal lookup. I can send a PR if that helps.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions