Skip to content

Default value introduced in subclass does not propagate to constructor #467

Closed

Description

@lawremi

This line drops the parent properties from the constructor by name; however, I think it's valid for subclasses to add/change the default for a parent property. Since we're dropping the subclass properties, the changed default does not make it to the constructor. Self args should probably be kept (and override the parent) if they have a different default.

Activity

  1. added a commit that references this issue on Oct 21, 2024
    a052343
  2. ltuijnder commented on May 2, 2025

    @ltuijnder

    Hi I too stumbled upon this issue.

    When I was refactoring a class (that had defaults in one of its properties) to be now a child class of a some template class that I want to use that also has this property. Suddenly my defaults were not working anymore!

    I should note that my parent template class does not set a default, nor does it have custom construction (which was flagged as a potential issue in #473 ). It is the simplest parent class you can imagine, yet it triggered this bug which was quite surprising with such simple class definitions that I had.

  3. lawremi commented on May 3, 2025

    @lawremi
    CollaboratorAuthor

    In my opinion, this is the most annoying issue with S7 at the moment. Would be good to resolve the concerns with the PR. I'm not sure I fully understand them.

  4. ltuijnder commented on May 5, 2025

    @ltuijnder

    @t-kalinowski In the PR you mentioned that it is tricky if the parent has a custom constructor. Because then the default value is implicitly different.

    But in my mind there is no issue. In case the parent has a custom constructor. it was already documented that the child should also define a custom constructor: https://rconsortium.github.io/S7/articles/classes-objects.html#constructors

    There’s one drawback of custom constructors that you should be aware of: any subclass will also require a custom constructor.

    And if a class has a custom constructor the user should automatically be aware defaults specified in parameter are ignored as the default argument of the constructor takes priority. Meaning the users needs to be explicit anyway with the defaults for its child class with the constructor. So there is no issue here.

    (Side note, maybe S7 could issue a warning if the user does provide a custom constructor and has default priorities set. Or elaborate more on the document of default in ?new_property)


    However there is still the issue that the default cannot be overwritten by a child class even if the parent class does not have a default constructor.

  5. added
    bug
    on May 21, 2026
  6. hadley commented on May 21, 2026

    @hadley
    Member

    Small example:

    library(S7)
    
    foo <- new_class("foo", properties = list(x = new_property(class_numeric, default = 1)))
    foo2 <- new_class("foo2", foo, properties = list(x = new_property(class_numeric, default = 2)))
    foo2()@x
  7. 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