RConsortium / S7

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

Include value in error message about invalid value from `register_method()` #460

Closed t-kalinowski closed 1 week ago

t-kalinowski commented 2 weeks ago

Closes #447

hadley commented 2 weeks ago

Hmmmm, I wonder if we should add a error wrapper in method<- since it has a bit more context. I think ideally I'd want

foo <- function() {}
method(foo, class_character) <- function(...) {}
#> Error: Failed to register <character> method for <foo> generic
#> `generic` is a function, but not an S3 generic function

But looking at the traceback, maybe we can't get the name of the generic because of the *tmp* substitution?

t-kalinowski commented 2 weeks ago

I don't think it's possible to get the original expression for x in a call like foo(x) <- through introspection.

> `foo<-` <- function(x, value) {
+   print(sys.calls())
+ }
> 
> x <- function() {}
> 
> foo(x) <- "some val"
[[1]]
`foo<-`(`*tmp*`, value = "some val")
hadley commented 1 week ago

Yeah, so the best we could do is mention the method. Maybe something like this?

foo <- function() {}
method(foo, class_character) <- function(...) {}
#> Error: Failed to register <character> method for generic
#> `generic`, function(...) is a function, but not a generic