-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -2,6 +2,10 @@ | |||
|
|||
## type join (closest common ancestor, or least upper bound) ## | |||
|
|||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
Null
will be able to opt into this mechanism.This is an alternative to #23940, though it doesn't address all issues since e.g.
collect
return 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:
Void
andNull
(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 toUnion
s of two types is still somewhat arbitrary.Finally, the name
isspecial
should really be improved but I couldn't find a good solution.