Skip to content

Union method registration overwrites explicit member methods #813

Backlog

Description

@hectorgray

Hi,

I'm working with new_union and found an effect similar to:

library(S7)

Pet <- new_union(
  Cat := new_class(),
  Dog := new_class()
)

Pet
#> <S7_union>: <Cat> or <Dog>

S7_inherits(Cat(), Pet)
#> [1] TRUE

speak := new_generic("pet")
method(speak, Cat) <- \(pet) "meow"

speak(Cat())
#> [1] "meow"
speak(Dog())
#> Error: Can't find method for `speak(<Dog>)`.

method(speak, Pet) <- \(pet) "..."
#> Overwriting method speak(<Cat>)

speak(Cat())
#> [1] "..."
speak(Dog())
#> [1] "..."

This surprised me and before reading #123 I believed it was a bug. Still it
does feel like a dispatch design issue.

Namely I believe that adding a method for a union should preserve any explicit
member method. The result of S7_inherits(Cat(), Pet) kind of reinforces this
expectation to me.

That is, speak(Cat()) should keep returning "meow" and only speak(Dog())
should return "...", regardless of registration order.

I don't have a ton of experience with OOP but a possible approach could be to
have S7 register a method for a class union as a fallback for its members?
This shouldn't affect any class hierarchies.

Would this be worth revisiting? I saw that giving explicit member methods
precedence came up in #123, and the overwriting notification was implemented a
few years ago now.

Other systems act closer to what I expect, e.g.:

R - S4

setClass("Cat", slots = c(name = "character"))
setClass("Dog", slots = c(name = "character"))
setClassUnion("Pet", c("Cat", "Dog"))

is(new("Cat"), "Pet")
#> [1] TRUE

setGeneric("speak", function(pet) standardGeneric("speak"))
#> [1] "speak"

setMethod("speak", "Cat", function(pet) "meow")
setMethod("speak", "Pet", function(pet) "...")

speak(new("Cat"))
#> [1] "meow"
speak(new("Dog"))
#> [1] "..."

Julia

struct Cat end
struct Dog end

const Pet = Union{Cat, Dog}
#> Pet (alias for Union{Cat, Dog})

Cat() isa Pet
#> true

speak(::Cat) = "meow"
#> speak (generic function with 1 method)

speak(::Pet) = "..."
#> speak (generic function with 2 methods)

speak(Cat())
#> "meow"

speak(Dog())
#> "..."

Activity

  1. hadley commented on Dec 2, 2026

    @hadley
    Member

    Hmm, it doesn't seem great that:

    method(speak, Cat) <- \(pet) "meow"
    method(speak, Pet) <- \(pet) "..."

    and

    method(speak, Pet) <- \(pet) "..."
    method(speak, Cat) <- \(pet) "meow"

    yield different results.

    OTOH we'd need to think about

    method(speak, Pet) <- \(pet) "..."
    method(speak, Pet) <- \(pet) ":::"
  2. hectorgray commented on Dec 2, 2026

    @hectorgray
    Author

    Is the concern that if union registration only fills in missing methods, the
    second Pet registration would leave Dog returning "..." when it should now
    return ":::"?

    Since method<- already receives Pet as an <S7_union>, could S7 keep track
    of which methods came from that union? Then re-registering Pet could update
    the method it supplied for Dog while leaving the explicit Cat method alone.

    To me the underlying issue seems to be the macro-like expansion at registration
    time. I was exploring a bit more and this might be throwing more fuel to the
    fire but for example:

    method(foo, Bar) <- f

    Reading the line alone won't tell us what effect the assignment will have, as
    it acts quite differently when Bar is a parent class vs. when Bar is a
    union.

    Cat := new_class()
    Dog := new_class()
    Shark := new_class()
    
    Pets <- Cat | Dog
    Animals <- Cat | Dog | Shark
    
    speak := new_generic("x")
    method(speak, Pets) <- \(x) "meow or woof"
    method(speak, Animals) <- \(x) stop("animals don't speak!")

    This may be a more natural use for unions than my first example; we deal with
    seemingly independent classes/unions, but the effect has the Animals method
    "overreaching" into the Pets method. Moreso, method(speak, Animals) <- NULL
    instead would outright de-register the Pets method.

    I tried looking at the source but I'm not yet familiar enough with dispatch
    internals to propose something confidently 😅. From my previous references
    though, S4 uses virtual superclasses while Julia keeps union signatures and
    compares specificity. The latter seems closer to S7, it lets a Pet method
    coexist with a Cat method without changing Cat's class hierarchy.

    Ref: https://docs.julialang.org/en/v1/devdocs/types/#Subtyping-and-method-sorting

  3. hectorgray commented on Dec 2, 2026

    @hectorgray
    Author

    Also this might be a more significant example for statistical programming:

    foo := new_generic("x")
    method(foo, class_integer) <- \(x) "int"
    method(foo, class_numeric) <- \(x) "num"
    #> Overwriting method foo(<integer>)
    foo(1L)
    #> [1] "num"
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions