Skip to content

Class unions #31

Closed
Closed

Description

@lawremi

The methods package defines class unions as new classes, inserted above the members. Messing with the class hierarchy causes all sorts of complications, and depends on multiple inheritance.

We could instead define class unions as instances, i.e., union(classA, classB) returns a ClassUnion object. Using a ClassUnion in a signature could define a separate method for each member of the union. Or, this could be encoded into the dispatch logic. Using a ClassUnion as a property type would tell the validator to return valid if the value is an instance of any of the members. I've seen this in Julia and TypeScript, and it's probably in many others. It probably helps that they're compiled.

TypeScript also has the notion of a type intersection, which makes sense given that it's entirely based on structural/duck typing. Not sure it applies in our case, particularly if we preclude multiple inheritance.

Activity

  1. hadley commented on Sep 25, 2020

    @hadley
    Member

    Do you have a few examples of where you'd use class unions? I have never used them.

  2. hadley commented on Sep 27, 2020

    @hadley
    Member

    Other possibly related ideas we might want to consider:

    • Traits: i.e. some way of declaring that class provides all of a set of methods
    • Sum types: which I think are sort of a cross between an enum (which is possibly what you're asking for in Literal types #32) and a union
  3. lawremi commented on Sep 28, 2020

    @lawremi
    CollaboratorAuthor

    My understanding is that sum type is just another term for class union.

  4. lawremi commented on Oct 2, 2020

    @lawremi
    CollaboratorAuthor

    Looks like I missed your question on use cases for type unions. Typically we've used them in cases where there are two types that behave the same (equivalent by duck typing), so we treat them the same. Like "vector U factor". Perhaps the most common case is allowing NULL for a slot when there is no obvious default value. Since "NULL" is a class, it's as simple as "MyClass U NULL".

  5. hadley commented on Oct 4, 2020

    @hadley
    Member

    So this:

    foobar <- newUnion("foobar", c("foo", "bar"))
    methods(mean, foobar) <- function(x) 10

    Is a shortcut for

    foobar <- newUnion("foobar", c("foo", "bar"))
    methods(mean, "foo") <- methods(mean, "bar") <- function(x) 10

    ?

  6. mmaechler commented on Oct 5, 2020

    @mmaechler
    Collaborator

    Well, there's much more about unions in S4: You want to have slots of objects of such a class union, so such slots can contain NULL - that's the use case I think Michael mostly talks about above.

  7. hadley commented on Oct 5, 2020

    @hadley
    Member

    In that case, couldn't the property validator also allow a vector of valid classes? If a class union is only needed in these two places, it seems simpler to me to special-case them, rather than introducing additional complexity into the class hierarchy.

  8. lawremi commented on Oct 5, 2020

    @lawremi
    CollaboratorAuthor

    The "vector of classes" is exactly what I was proposing. We would call those "unions" and use a formal object to represent them, but they would not affect the class hierarchy like they do in S4.

  9. hadley commented on Oct 5, 2020

    @hadley
    Member

    Ok, got it. So how do they fit into the object hierarchy? Any where you can provide a Class you can also provide a ClassUnion? Or can they be implemented purely by defining a single method somewhere?

    ClassUnion <- newClass("ClassUnion", properties = list("classes = "character"))
    method(inherits, ClassUnion) <- function(x, what) {
      any(sapply(what@classes, inherits, x))
    }

    It feels weird to make something like inherits() a generic.

  10. hadley commented on Oct 5, 2020

    @hadley
    Member

    I guess I also wonder if it's worth making it a formal class, rather than simply allowing the default property validator to take a list of classes. It seems like you'd want to use the idea of a class union in 3+ places in order to make it worthwhile as a top-level concept.

  11. lawremi commented on Oct 6, 2020

    @lawremi
    CollaboratorAuthor

    Having an explicit constructor would help clarify code, validate, etc, and it would likely be useful internally. We could also accept a simple list. It's similar to how the methods package has a signature() constructor and corresponding class but also accepts a character vector.

  12. added a commit that references this issue on Oct 7, 2020
    784b0f3
  13. hadley commented on Oct 7, 2020

    @hadley
    Member

    Given that a union with NULL is one of the most common operations, do you think it makes sense to provide an optional helper? e.g.

    Optional <- function(class) {
      ClassUnion(class, "NULL")
    }
  14. lawremi commented on Oct 7, 2020

    @lawremi
    CollaboratorAuthor

    I like the convenience. Will this be obvious enough? Perhaps someone would assume that the property could be undefined, i.e., the attribute or whatever would just not be there.

  15. hadley commented on Oct 7, 2020

    @hadley
    Member

    @lawremi It will be very obvious to Haskell and Rust programmers 🤣. Another possible name is Nullable(), but I think Optional() is reasonably clear, and it'd be reinforced by a NULL as a default value in the constructor.

  16. lawremi commented on Oct 7, 2020

    @lawremi
    CollaboratorAuthor

    Nullable() is definitely more obvious to me.

  17. mmaechler commented on Oct 8, 2020

    @mmaechler
    Collaborator

    I see pros and cons for both Optional() and Nullable()
    O.: + well known English, no doubt about writing from hearing
    - quite generic and (partly consequently) ambiguous: in some contexts it will be used, notably method / signature definition, it could be understood as an/the indication of an optional function argument and often it will "correlated" with that concept, where it may be better to disambiguate

    N: - most R users will never have seen this as an adjective/adverb in English; when hearing, it's easily confused with malleable.
    + its more specific than 'Optional()' and those (majority of) R users who've learned about NULL (and that NULL is often default value in function arguments) will more easily recall what it means exactly.

    Inspite of the fact the numbers of characters I've written would favor Nullable over Optional, that's not the case. Teaching and understanding (and remembering some details of) the new system is important, too. ..

  18. hadley commented on Oct 8, 2020

    @hadley
    Member

    Random thought — I think you'd only want to use Nullable/Optional in properties. You wouldn't want to use it in method definition because you'd be defining a method on NULL, which is probably not what you mean (that might make sense in S4, but it's less likely to make sense here). So there's maybe less possibility for confusion because it will only appear in formal property definitions, which most users won't see very often.

  19. added a commit that references this issue on Oct 16, 2020
    516441f
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