Skip to content

should constructor, validator, getter, setter... be enclosed in baseenv() #553

@moodymudskipper

Description

@moodymudskipper

This would make the S7 objects simpler, pointing only to named environment. My understanding is that a user fetching values from parent environments through lexical scoping would almost always be a mistake. An aspect I really like about S7 is that everything is contained in the object itself, the current enclosure system undermines this.

IIRC in R6 the enclosure is updated.

base classes currently use the enclosure could be adapted. For instance we can have :

class_logical <- list(
  class = "logical",
  constructor_name = "logical",
  constructor = (function(.data = logical(0)) {
    .data
  }) |>
    (`environment<-`)(baseenv()),
  validator = (function(object) {
    if (base_class(object) != "logical") {
      sprintf("Underlying data must be <logical> not <%s>", base_class(object))
    }
  }) |>
    (`environment<-`)(baseenv())
) |>
  structure(class = "S7_base_class")

Right now there is no code class_logical and friends are "different" in every session.

they could be enclosed in topenv() too if we want to be able to use helpers in the NAMESPACE.

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