Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R7 does not seem to work with class unions #72

Closed
sanchit-saini opened this issue Jun 15, 2021 · 8 comments · Fixed by #150
Closed

R7 does not seem to work with class unions #72

sanchit-saini opened this issue Jun 15, 2021 · 8 comments · Fixed by #150

Comments

@sanchit-saini
Copy link

> bar <- new_generic(name = "bar", signature = c("x", "y"))
> setClassUnion("char_OR_NULL", c("character", "NULL"))
> method(bar, list("char_OR_NULL", "numeric")) <- function(x, y, ...) paste0("foo-", x, ":", y)
> bar("foo", 1)
Error: No method found!
> bar(NULL, 1)
Error: No method found!
@hadley
Copy link
Member

hadley commented Oct 22, 2021

Reprex:

library(R7)

bar <- new_generic(name = "bar", signature = c("x", "y"))
setClassUnion("char_OR_NULL", c("character", "NULL"))
method(bar, list("char_OR_NULL", "numeric")) <- function(x, y, ...) paste0("foo-", x, ":", y)
bar("foo", 1)
#> Error: No method found!
bar(NULL, 1)
#> Error: No method found!

Created on 2021-10-22 by the reprex package (v2.0.1)

@jimhester
Copy link
Collaborator

It does work with R7 unions FWIW, e.g.

library(R7)

bar <- new_generic(name = "bar", signature = c("x", "y"))
chr_or_null <- new_union("character", "NULL")
method(bar, chr_or_null) <- function(x, y, ...) paste0("foo-", x, ":", y)
bar("foo", 1)
#> [1] "foo-foo:1"
bar(NULL, 1)
#> [1] "foo-:1"

Created on 2021-10-26 by the reprex package (v2.0.1)

I am not sure it should work with S4 class unions?

@hadley
Copy link
Member

hadley commented Oct 26, 2021

If (e.g.) Bioc wanted to shift from S4 to R7, I suspect it would be useful to be able to switch generics while leaving the class structure as is.

Should method(bar, list("char_OR_NULL", "numeric")) <- warn that char_OR_NULL isn't known class?

@mmaechler
Copy link
Collaborator

I agree it would be nice and possibly important to have "glue functionality" providing compatibility. If this (partial S4 support) seems a bit unclean, might it be a possibilty to have the functionality "modularized" in an extra S4toR7 or R7forS4 package which would depend on methods and R7 ?

@hadley
Copy link
Member

hadley commented Jan 18, 2022

Revisit after #134

@hadley
Copy link
Member

hadley commented Jan 27, 2022

With #134

library(R7)
char_OR_NULL <- setClassUnion("char_OR_NULL", c("character", "NULL"))

foo <- new_generic("foo", dispatch_args = "x")
method(foo, char_OR_NULL) <- function(x) "char_or_NULL"

foo("foo", 1)
#> Error: Can't find method for generic `foo()` with classes:
#> - x: <character>
foo(NULL, 1)
#> Error: Can't find method for generic `foo()` with classes:
#> - x: <NULL>

Created on 2022-01-26 by the reprex package (v2.0.1)

I think the easiest way to deal with this will be to convert S4 unions to R7 unions, so the same method dispatch logic can apply. Does this seem like the right way to get the classes that an S4 union applies to?

Foo <- setClass("Foo", slots = "x")
setClassUnion("optionalFoo", c("Foo", "NULL"))

union <- getClass("optionalFoo")
subclasses <- Filter(function(x) x@distance == 1, union@subclasses)
lapply(subclasses, function(x) getClass(x@subClass))
#> $`NULL`
#> Class "NULL" [package "methods"]
#> 
#> No Slots, prototype of class "NULL"
#> 
#> Extends: "OptionalFunction", "optionalMethod", "optionalFoo"
#> 
#> Known Subclasses: 
#> Class ".NULL", directly, with explicit coerce
#> 
#> $Foo
#> Class "Foo" [in ".GlobalEnv"]
#> 
#> Slots:
#>           
#> Name:    x
#> Class: ANY
#> 
#> Extends: "optionalFoo"

Created on 2022-01-26 by the reprex package (v2.0.1)

Translating S4 unions to R7 unions also means that we need to be able to translate the S4 wrappers of base types to the R7 wrappers of base types, i.e. we need to be able to translate getClass("character") to "character". From a quick inspection, it doesn't look like you can determine this from the class metadata, so I guess we just need to do some matching based on @name? (assuming the @package is "methods").

@lawremi
Copy link
Collaborator

lawremi commented Feb 1, 2022

The overall approach sounds good to me. For finding the members of the union, it may be better to recurse instead in order to handle nested unions.

hadley added a commit that referenced this issue Feb 8, 2022
hadley added a commit that referenced this issue Feb 8, 2022
@hadley
Copy link
Member

hadley commented Feb 8, 2022

Now

library(R7)
char_OR_NULL <- setClassUnion("char_OR_NULL", c("character", "NULL"))

foo <- new_generic("foo", dispatch_args = "x")
method(foo, char_OR_NULL) <- function(x) "char_or_NULL"

foo("foo")
#> [1] "char_or_NULL"
foo(NULL)
#> [1] "char_or_NULL"

Created on 2022-02-08 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants