Skip to content

Child class property setters that mask parent class properties are unused at construction #585

Closed

Description

@dgkf

Given a set of child-parent classes, where the child masks a parent property and adds a custom setter such as

parent <- new_class(
  "parent",
  properties = list(
    "x" = class_integer,
    "y" = class_character,
    "z" = class_any
  )
)

child <- new_class(
  "child",
  parent = parent,
  properties = list(
    "z" = new_property(
      class_double,
      setter = function(self, value) {
        stop("hey! don't set that!")
        self
      }
    )
  )
)

Calling the child constructor does not ever hit the child property setter.

child(z = 1)
<child>
 @ x: int(0)
 @ y: chr(0)
 @ z: num 1

I would expect that calling child(z = 1) would use the child property setter and throw the error, but it seems that the parent constructor is used then the value is type checked against the child property class without ever using the child setter.

Activity

  1. dgkf commented on Dec 16, 2025

    @dgkf
    ContributorAuthor

    I spent some time investigating this. At least as far as I've interpreted the intended use cases, there seems to be two contradictory goals:

    1. properties should be able to be specialized in child classes (child property behaviors mask parent behaviors)
    2. constructors should be hierarchical (constructing a parent object first, then apply child constructors)

    Assuming we don't want to totally throw a wrench in the current hierarchical construction model, I think the best approach would be to ignore setter functions during construction of a new object -- similar to how prop<- is called with check = FALSE so that the object is only validated at the end of construction. Similarly, we could then "set" each value with whatever value it received at that point to trigger the individual setter functions.

    In this way, the constructors still get evaluated bottom up (first calling parent constructors and then the child constructors) without triggering setter functions, then the setters are evaluated top down (giving priority to children that mask parent properties).

    This would require disabling setters and object validation for any parent constructors when an object is constructed, then running setters following masking rules, then finally doing object validation recursively.

  2. hadley commented on Dec 17, 2025

    @hadley
    Member

    I think this is probably the same basic issue as #570/#467.

  3. dgkf commented on Dec 17, 2025

    @dgkf
    ContributorAuthor

    Was interested in exploring this a bit more and made a fix over in dgkf/S7. It's a substantial change to the way that object construction (new_object()) works which certainly requires more detailed review before being considered so I just left it over there and tried to annotate the changes with rationale.

    This would address the issue with the setters as well as the default values mentioned in the other issues.

    It is nearly non-breaking. There are some minor edge cases that affect custom constructors which rely on positional arguments to new_object(). I think that these are necessary breakages to support property masking.

  4. added
    bug
    on May 21, 2026
  5. added a commit that references this issue on Jun 24, 2026
    a4d4f49
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

    bugconstruction 🚧

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions