You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
method(convert, list(class_any, class_character)) <-function(from, to, ...) as.character(from)
method(convert, list(class_any, class_integer)) <-function(from, to, ...) as.integer(from)
method(convert, list(class_any, class_logical)) <-function(from, to, ...) as.logical(from)
...
One thing to consider is that individual package authors may want different behavior. E.g., a strict whole-number check when casting double to integer
method(convert, list(class_double, class_integer)) <-function(from, to, ...) {
stopifnot("must be a whole number"= trunc(from) ==from)
as.integer(from)
}
We don't want different packages exporting different methods for this type of cast. We may want to dis-allow setting convert(, to) methods for base S3 classes for this reason.
I worry that this has a bunch of unintended consequences that are hard to reason about. In general, I think it's best to be as explicit as possible with coercion, even if it means some existing functions might not work.
In general, I think it's ok to rely on the convention that you should only define methods if you own either one of the classes involved or the generic. It would be nice if we could automate that as a check, but I suspect it's going to be hard.
I think we have to choose between #547 and this — so the question is what principle determines whether we should have the S3 generics call the S7 generics, or the S7 generics call the S3 generic?
The above solution ensures broadest compatibility, including all the non-S7 objects that currently define methods on the as.* primitives. Basically, as.* came first, so it's at the bottom.
Activity
fithisux commentedon Oct 24, 2024
@t-kalinowski if convert method takes always 2 arguments, why do we need the
list(_,_)part?t-kalinowski commentedon Oct 24, 2024
The
list(_,_)is the second argument tomethod<-and is used by the generic to perform dispatch based on the argument types. https://rconsortium.github.io/S7/reference/method-set.htmlhadley commentedon Oct 24, 2024
I worry that this has a bunch of unintended consequences that are hard to reason about. In general, I think it's best to be as explicit as possible with coercion, even if it means some existing functions might not work.
In general, I think it's ok to rely on the convention that you should only define methods if you own either one of the classes involved or the generic. It would be nice if we could automate that as a check, but I suspect it's going to be hard.
lawremi commentedon Oct 26, 2024
I've already defined all of the base/S3 conversions in my S7-based package. Definitely useful to have.
fithisux commentedon Oct 26, 2024
You are right.
hadley commentedon May 22, 2026
I think we have to choose between #547 and this — so the question is what principle determines whether we should have the S3 generics call the S7 generics, or the S7 generics call the S3 generic?
lawremi commentedon May 22, 2026
The above solution ensures broadest compatibility, including all the non-S7 objects that currently define methods on the
as.*primitives. Basically,as.*came first, so it's at the bottom.Add `convert()` fallback to base `as.*` methods (#664)