RConsortium / S7

S7: a new OO system for R
https://rconsortium.github.io/S7
Other
386 stars 32 forks source link

Several unexpected behaviours #374

Closed jl5000 closed 10 months ago

jl5000 commented 10 months ago

I've been using S7 to create different types of parent and child classes and I'm seeing some unexpected results.

I create class_a below which works as expected, apart from the unhelpful error message when trying to set a read-only property.

When creating class_b I am expecting the property definition of the parent class to be overwritten, allowing x to be undefined (and therefore an empty chr vector). However it complains that it is NULL. when I try setting it to a non-empty value it still errors.

library(S7)

class_a <- new_class("class_a",
                     properties = list(
                       x = new_property(class_character,
                                        getter = function(self) "a",
                                        validator = function(value){
                                          if(length(value) == 0)
                                            "Error"
                                        })
                     ))

class_a()
#> <class_a>
#>  @ x: chr "a"
class_a("b")
#> Error in class_a("b"): unused argument ("b")

class_b <- new_class("class_b", parent = class_a,
                     properties = list(
                       x = class_character
                     )
)

class_b()
#> Error: <class_b> object properties are invalid:
#> - @x must be <character>, not <NULL>
class_b("b")
#> Error in class_b("b"): unused argument ("b")

Created on 2023-10-08 with reprex v2.0.2

hadley commented 10 months ago

I don't think we can create a better error "unused argument" because class_a() is a regular function with one argument for each settable property.

The second problem is really #352; it shouldn't be possible to override properties in subclasses (at least in the way that properties are implemented in S7).