-
-
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
WIP: Add promote_strict mechanism and use it instead of typejoin() #25423
Conversation
…ppropriate Introduce a mechanism similar to promote(), but used to return types which can hold exactly all values of the input types. Use it instead of typejoin() to choose an appropriate element type with collect(), map(), broadcast(), and Dict(). Add appropriate methods for Number types, as well as for Nothing/Missing so that Union{T, Nothing/Missing} is used instead of Any. Add promotion rules for Tuple and NamedTuple so that promotion is performed element by element, to allow for more precise typing of fields, potentially helping the compiler down the road.
I'm not a specialist in promotion rules, just a thought: what if it's implemented as |
I had a similar thought, @alyst. I guess I would call it |
26002ff
to
0fa0040
Compare
Good idea. That also reduces code duplication. I've retained I've made a lot of changes, the PR should be ready for a serious review now. At first I was tempted to apply automatically to all types the new logic which transforms unions of parametric types into types with So now this behavior is opt-in, via |
Do this only for NamedTuple (and Tuple since typejoin() already handles it). Else types may not implement necessary conversions, which triggers errors.
0fa0040
to
7b3ae51
Compare
I don't think we want a separate set of numeric promotion rules. Judging from past experience, people will not like it if combining e.g. I think this should only be for containers; basically it should only change type parameters and not data representations. In fact NamedTuple is the only use case for this I'm aware of so far. But we might want to expand it to containers generally, and use existing promotions only for arithmetic. For instance we should maybe stop doing this:
given that with different types this happens:
|
Actually, it's possible we really don't need arithmetic-style promotion rules for arrays anymore, since broadcast seems to automatically handle it (through a process that bottoms out at scalar arithmetic). |
The range hashing code added recently needed exactly such a strict numeric promotion mechanism. We settled for calling |
I don't think that's true; that code needed a non-overflowing subtraction ( In this context I want an even stricter promotion mechanism that preserves type information as well as values. And we don't want to go back to the world of issues like #3274, #14252, and all the unsigned stuff. |
@JeffBezanson I don't see what you mean. We only use this in the context of containers (via Do you suggest we use exact promotion everywhere, i.e. that we (more or less) mandate that promotion methods should not lose precision? This could be OK for array concatenation, but how would scalar arithmetic choose the return type of operations? The range hashing code indeed needs something else, though the new |
I'm saying it should only differ from
No, I think scalar arithmetic should stay as-is. But we might want to remove the |
Sorry, I still don't get it. Isn't the whole point that
Fine with me. I don't have use cases in mind for either choice. |
Right, we would need definitions for |
So, concretely, you suggest removing all methods on |
See #25553 for a more limited alternative which doesn't add any new public API, and uses the internal |
Bump! EDIT: I wanted to bump #25553 instead, but that's really the same issue. |
Closing since #25553 has been merged. |
Introduce a mechanism similar to
promote
, but used to return types which can hold exactly all values of the input types. Use it instead oftypejoin
to choose an appropriate element type withcollect
,map
,broadcast
, andDict
.Add appropriate methods for Number types, as well as for
Nothing
/Missing
so thatUnion{T, Nothing/Missing}
is used instead ofAny
. Add promotion rules forTuple
andNamedTuple
so that promotion is performed element by element, to allow for more precise typing of fields, potentially helping the compiler down the road.See summary in this Discourse post. This replaces #24332 for the goal of improving typing and performance when working with missing values. This is very preliminary (no docs in the manual, methods missing for some types, missing tests...), but I think this illustrates the approach and will probably expose many mistakes I'm making. Here's in particular what this achieves (on current master the type is
NamedTuple{(:a, :b),T} where T<:Tuple
):I don't particularly like the
promote_strict
term; we could also reusewiden
, though keepingpromote
in the name makes the relationship withpromote
clearer (which makes sense in particular because of the fallback frompromote
topromote_strict
).