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
{{ message }}
Repository navigation
Union method registration overwrites explicit member methods #813
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.
struct Cat endstruct Dog endconst Pet = Union{Cat, Dog}
#> Pet (alias for Union{Cat, Dog})Cat() isa Pet
#> truespeak(::Cat) ="meow"#> speak (generic function with 1 method)speak(::Pet) ="..."#> speak (generic function with 2 methods)speak(Cat())
#> "meow"speak(Dog())
#> "..."
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.
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.
Activity
hadley commentedon Dec 2, 2026
Hmm, it doesn't seem great that:
and
yield different results.
OTOH we'd need to think about
hectorgray commentedon Dec 2, 2026
Is the concern that if union registration only fills in missing methods, the
second
Petregistration would leaveDogreturning "..." when it should nowreturn ":::"?
Since
method<-already receivesPetas an<S7_union>, could S7 keep trackof which methods came from that union? Then re-registering
Petcould updatethe method it supplied for
Dogwhile leaving the explicitCatmethod 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:
Reading the line alone won't tell us what effect the assignment will have, as
it acts quite differently when
Baris a parent class vs. whenBaris aunion.
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
Animalsmethod"overreaching" into the
Petsmethod. Moreso,method(speak, Animals) <- NULLinstead would outright de-register the
Petsmethod.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
Petmethodcoexist with a
Catmethod without changingCat's class hierarchy.Ref: https://docs.julialang.org/en/v1/devdocs/types/#Subtyping-and-method-sorting
hectorgray commentedon Dec 2, 2026
Also this might be a more significant example for statistical programming: