RConsortium / S7

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

Using `class` property seems to clash with object's class #425

Closed jmbarbone closed 2 weeks ago

jmbarbone commented 2 months ago
library(S7)
packageVersion("S7")
#> [1] '0.1.1.9000'

Foo <- new_class(
  "Foo",
  properties = list(
    class = new_property(class_character, default = "Class"),
    other = new_property(class_character, default = "Other")
  )
)

x <- Foo()

x@other
#> [1] "Other"
x@class # retrieves information about x
#> [1] "Foo"       "S7_object"
class(x)
#> [1] "Foo"       "S7_object"

# sets fine
x@other <- "New"
x@other
#> [1] "New"

# fails
try({
  x@class <- "New"
  x@class
})
#> Error : `object` must be an <S7_object>, not a S3<New>

Created on 2024-08-26 with reprex v2.1.1

lawremi commented 2 months ago

If we end up making @() a general attribute getter for any R object, then we would want to reserve the class property name. For now, I'd suggest using a different name.

t-kalinowski commented 1 month ago

We should check for "reserved" property names and error in new_class(). Besides class, others that I imagine might produce problems: dim, names, dimnames, tsp, levels (for factor)