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
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.
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".
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.
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.
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.
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?
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.
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.
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.
@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.
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. ..
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.
Activity
hadley commentedon Sep 25, 2020
Do you have a few examples of where you'd use class unions? I have never used them.
hadley commentedon Sep 27, 2020
Other possibly related ideas we might want to consider:
lawremi commentedon Sep 28, 2020
My understanding is that sum type is just another term for class union.
lawremi commentedon Oct 2, 2020
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
NULLfor a slot when there is no obvious default value. Since "NULL" is a class, it's as simple as "MyClass U NULL".hadley commentedon Oct 4, 2020
So this:
Is a shortcut for
?
mmaechler commentedon Oct 5, 2020
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.hadley commentedon Oct 5, 2020
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.
lawremi commentedon Oct 5, 2020
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.
hadley commentedon Oct 5, 2020
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?
It feels weird to make something like
inherits()a generic.hadley commentedon Oct 5, 2020
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.
lawremi commentedon Oct 6, 2020
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.Define class unions
hadley commentedon Oct 7, 2020
Given that a union with
NULLis one of the most common operations, do you think it makes sense to provide an optional helper? e.g.lawremi commentedon Oct 7, 2020
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.
hadley commentedon Oct 7, 2020
@lawremi It will be very obvious to Haskell and Rust programmers 🤣. Another possible name is
Nullable(), but I thinkOptional()is reasonably clear, and it'd be reinforced by aNULLas a default value in the constructor.lawremi commentedon Oct 7, 2020
Nullable()is definitely more obvious to me.mmaechler commentedon Oct 8, 2020
I see pros and cons for both
Optional()andNullable()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. ..
hadley commentedon Oct 8, 2020
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.Define class unions (#46)