RConsortium / S7

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

Default property values aren't used when objects are created with a constructor #405

Open guslipkin opened 2 months ago

guslipkin commented 2 months ago

If you use a default value for a property and a constructor for creating the object, the class check is applied before the NULL check resulting in this message

Error: <class> object properties are invalid:
  - @prop must be S3<base_class>, not <NULL>

This leaves users the only option of setting a default value in the constructor call itself which feels clunky and defeats the purpose of the default option.

person <- S7::new_class(
  name = 'person',
  properties = list(
    'name' = S7::new_property(
      class = S7::class_character
    ),
    'birthdate' = S7::new_property(
      class = S7::class_Date,
      default = as.Date('1993-08-01')
    )
  ),
  constructor = \(name = 'R. Language', birthdate = NULL) {
    S7::new_object(
      S7::S7_object(),
      'name' = name,
      'birthdate' = birthdate
    )
  }
)

person()