Skip to content

Default convert() methods for base classes.  #472

Closed

Description

@t-kalinowski

Users may expect something like this to work:

x <- 3
convert(x, class_character)

Concretely, I wonder if we should do:

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.

Activity

  1. fithisux commented on Oct 24, 2024

    @fithisux

    @t-kalinowski if convert method takes always 2 arguments, why do we need the list(_,_) part?

  2. t-kalinowski commented on Oct 24, 2024

    @t-kalinowski
    MemberAuthor

    The list(_,_) is the second argument to method<- and is used by the generic to perform dispatch based on the argument types. https://rconsortium.github.io/S7/reference/method-set.html

  3. hadley commented on Oct 24, 2024

    @hadley
    Member

    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.

  4. lawremi commented on Oct 26, 2024

    @lawremi
    Collaborator

    I've already defined all of the base/S3 conversions in my S7-based package. Definitely useful to have.

    method(convert, list(class_any, class_logical)) <-
        function(from, to, ...) as.logical(from, ...)
    method(convert, list(class_any, class_integer)) <-
        function(from, to, ...) as.integer(from, ...)
    method(convert, list(class_any, class_double)) <-
        function(from, to, ...) as.double(from, ...)
    method(convert, list(class_any, class_complex)) <-
        function(from, to, ...) as.complex(from, ...)
    method(convert, list(class_any, class_character)) <-
        function(from, to, ...) as.character(from, ...)
    method(convert, list(class_any, class_raw)) <-
        function(from, to, ...) as.raw(from, ...)
    method(convert, list(class_any, class_list)) <-
        function(from, to, ...) as.list(from, ...)
    method(convert, list(class_any, class_expression)) <-
        function(from, to, ...) as.expression(from, ...)
    method(convert, list(class_any, class_function)) <-
        function(from, to, ...) as.function(from, ...)
    method(convert, list(class_any, class_environment)) <-
        function(from, to, ...) as.environment(from, ...)
    method(convert, list(class_any, class_name)) <-
        function(from, to, ...) as.name(from, ...)
    method(convert, list(class_any, class_call)) <-
        function(from, to, ...) as.call(from, ...)
    
    method(convert, list(class_any, class_data.frame)) <-
        function(from, to, ...) as.data.frame(from, ...)
    method(convert, list(class_any, class_Date)) <-
        function(from, to, ...) as.Date(from, ...)
    method(convert, list(class_any, class_factor)) <-
        function(from, to, ...) as.factor(from, ...)
    method(convert, list(class_any, class_POSIXct)) <-
        function(from, to, ...) as.POSIXct(from, ...)
    method(convert, list(class_any, class_POSIXlt)) <-
        function(from, to, ...) as.POSIXlt(from, ...)
    method(convert, list(class_any, class_formula)) <-
        function(from, to, ...) as.formula(from, ...)
  5. fithisux commented on Oct 26, 2024

    @fithisux

    The list(_,_) is the second argument to method<- and is used by the generic to perform dispatch based on the argument types. https://rconsortium.github.io/S7/reference/method-set.html

    You are right.

  6. added
    feature
    on May 21, 2026
  7. hadley commented on May 22, 2026

    @hadley
    Member

    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?

  8. lawremi commented on May 22, 2026

    @lawremi
    Collaborator

    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.

  9. added a commit that references this issue on May 30, 2026
    c170a63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coercion 📢feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions