Add special case for Void in typejoin() to return Union{T, Void}#24332
Add special case for Void in typejoin() to return Union{T, Void}#24332
Conversation
|
Maybe call this |
|
Except that it's not as general as a rule: it's very limited since it only allows returning |
vtjnash
left a comment
There was a problem hiding this comment.
There's nothing wrong with changing this function in Base, but we're currently using the same code in Core.Inference, and we don't want that usage to change. (it would be perfectly acceptable however to copy this function and have different versions in use, or do something like create a promote_join that is overloadable and calls typejoin as a fallback, and use it instead everywhere except inference)
base/promotion.jl
Outdated
|
|
||
| isspecial(::Type) = false | ||
| isspecial(::Type{Void}) = true | ||
| isspecialpair(a, b) = isconcrete(a) && isconcrete(b) && (isspecial(a) || isspecial(b)) |
There was a problem hiding this comment.
If you're intending for people to override this, typejoin won't be pure anymore (currently marked as such)
There was a problem hiding this comment.
Even if people are only supposed to override this with isspecial(::Type{Null}) = true? @pure is still a mystery to me (is the problem due to the addition of methods after the fact?).
There was a problem hiding this comment.
is the problem due to the addition of methods after the fact?
Yes.
Thanks for the suggestions. I really don't have strong opinions about what's the best approach. At this point I'm not sure there's a use case for a complex |
|
@JeffBezanson Any suggestions regarding the best approach? |
|
I prefer doing the most conservative thing and only adding this for |
|
Fine with me, will have a look once |
…, Void/Missing} This allows for a more efficient code and array layout than using Any.
31f9407 to
d5f0769
Compare
…priate typejoin() is now used only for inference, and promote_join() everywhere else to choose the appropriate element type for a collection.
d5f0769 to
9094694
Compare
|
I've updated the PR. I now leave |
|
@JeffBezanson @vtjnash Is the new approach OK? |
|
Looks like this needs a rebase; @JeffBezanson, mind taking a look? |
|
Closing in favor of the alternative |
|
GitHub won't let me reopen this, so I've filed #25553 instead. |
This allows for a more efficient code and array layout than using
Any.Other types like
Nullwill be able to opt into this mechanism.This is an alternative to #23940, though it doesn't address all issues since e.g.
collectreturn types are not always inferred here even though they are correct thanks to the progressive broadening of the container type (compare tests with the other PR).That's also the minimal change among multiple options:
VoidandNull(this PR)The last solution is less arbitrary, but things like
[1, "a"]would return aVector{Union{String, Int}}, and the fact that this applies only toUnions of two types is still somewhat arbitrary.Finally, the name
isspecialshould really be improved but I couldn't find a good solution.