Skip to content

Method dispatch for class unions #123

Closed

Description

@hadley

Say we have the following class union:

library(R7)

cls_1 <- new_class("cls_1")
cls_1_1 <- new_class("cls_1_1", cls_1)
cls_2 <- new_class("cls_2")
cls_2_1 <- new_class("cls_2_1", cls_2)

u <- new_union(cls_1_1, cls_2_1)

In which order does method dispatch occur?

  • Is it breadth-first, i.e. cls_1_1, cls_2_1, cls_1, cls_2, R7_object ?
  • Is it depth-first, i.e. cls_1_1, cls_1, R7_object, cls_2_1, cls_2

It seems desirable for R7_object to come last, so this suggests breadth first?

Activity

  1. jimhester commented on Nov 15, 2021

    @jimhester
    Collaborator

    I think it currently just loops over the classes in each member of the union, which would be depth-first, but I agree breadth first does seem to make the most sense.

  2. jimhester commented on Nov 15, 2021

    @jimhester
    Collaborator

    Also I don't think there is currently any check for duplicate classes, so R7_object probably currently gets registered twice, with the second registration overwriting the first.

  3. lawremi commented on Nov 25, 2021

    @lawremi
    Collaborator

    Not sure I understand this question. We wouldn't have an instance of a union, right? Is this for methods with a union in their signature? But wouldn't they have equal affinity for each member of the union?

  4. hadley commented on Nov 25, 2021

    @hadley
    MemberAuthor

    Right, in method dispatch, unions are a shortcut, so instead of writing (e.g.)

    method(mean, "integer") <- function(x) sum(x) / length(x)
    method(mean, "double") <- function(x) sum(x) / length(x)

    You can write:

    numeric <- new_union("integer", "double")
    method(mean, numeric) <- function(x) sum(x) / length(x)

    But I guess the problem only occurs if you mix dispatch on unions with dispatch on classes, so we might be able resolve this by warning when you override an existing method?

  5. lawremi commented on Dec 13, 2021

    @lawremi
    Collaborator

    Or we just define a rule that a method for a member of a union takes precedence over a method for that union.

  6. mmaechler commented on Dec 13, 2021

    @mmaechler
    Collaborator

    Or we just define a rule that a method for a member of a union takes precedence over a method for that union.

    Yes, this would really intuitive, notably for people who already know the traditional S3 and S4 group methods.

  7. hadley commented on Dec 13, 2021

    @hadley
    MemberAuthor

    Ok, I'll have to think about how to implement that; currently defining a method for a union just defines methods for the individual classes.

    That also raises the general question of what happens when you override an existing method. Currently it is silently overridden — that's probably what you want for interactive development, but not what you want when loading multiple packages.

    library(R7)
    
    foo <- new_class("foo")
    bar <- new_generic("bar", signature = "x")
    
    method(bar, foo) <- function(x, ...) 1
    method(bar, foo) <- function(x, ...) 2
    
    bar(foo())
    #> [1] 2

    Created on 2021-12-13 by the reprex package (v2.0.1)

  8. ltierney commented on Dec 13, 2021

    @ltierney
    Collaborator
  9. hadley commented on Feb 24, 2022

    @hadley
    MemberAuthor

    In R7, method dispatch for unions is handled at method registration time, rather than at method dispatch time like S4. I think this approach is a bit simpler because it means that creating a R7 union does not affect the class hierarchy of existing R7 classes in the way that creating a S4 union does. This means that we can't prefer explicit classes over unions in general.

    For now, I've added a message so that you at least know when you're overriding a method, and we can revisit sealing in the future.

  10. added a commit that references this issue on Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions