Skip to content

Provide base as.*() methods for S7_object? #547

Closed

Description

@teunbrand

This is just an idea, if it won't get implemented I won't be disappointed.
The idea is that S7 provides methods for S7 objects for the coersion generics in base R.
They can use convert() with prepopulated to arguments.

library(S7)

as.list.S7_object <- function(x) convert(x, class_list)
as.character.S7_object <- function(x) convert(x, class_character)
# also as.numeric, as.factor, etc.

That way, you get this coersion 'for free' if you implement convert() methods.

foo <- new_class("foo")

method(convert, list(foo, class_list)) <- function(from, to) list("foo")

# Works out of the box
as.list(foo())
#> [[1]]
#> [1] "foo"

# convert is not defined for 'foo'
as.character(foo())
#> Error: Can't find method for generic `convert()` with dispatch classes:
#> - from: <foo>
#> - to  : <character>

Created on 2025-06-19 with reprex v2.1.1

Activity

  1. added
    feature
    and removed on May 21, 2026
  2. hadley commented on May 22, 2026

    @hadley
    Member

    I think Michael's argument in #472 is compelling; S7 builds on top of S3, so convert() should be defined in terms of as.*() not the other way around.

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