- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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: Allow convert and reinterpret for Arrays of unions with missing/nothing #36186
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,33 @@ convert(::Type{T}, x::T) where {T>:Union{Missing, Nothing}} = x | |
convert(::Type{T}, x) where {T>:Missing} = convert(nonmissingtype_checked(T), x) | ||
convert(::Type{T}, x) where {T>:Union{Missing, Nothing}} = convert(nonmissingtype_checked(nonnothingtype_checked(T)), x) | ||
|
||
for Cs in ((:Missing,), (:Nothing,), (:Missing, :Nothing)) | ||
@eval function Base.reinterpret( | ||
::Type{<:T}, # Note this is effectively same as Type{T} since will error if T is abstract | ||
xs::Array{Union{T, $(Cs...)},N} | ||
) where {T,N} | ||
isbitstype(T) || throw(ArgumentError("cannot reinterpret `$(eltype(xs))`, type `$(T)` is not a bits type")) | ||
return unsafe_wrap(Array{T,N}, Ptr{T}(pointer(xs)), length(xs)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idk, I didn't even know we did aliasing detection. |
||
end | ||
|
||
#== | ||
@eval function Base.convert( | ||
::Type{Array{Q,N}}, | ||
xs::Array{Union{T, $(Cs...)},N} | ||
) where {N} where {T<:Q} where Q | ||
|
||
all(x->x isa T, xs) || throw(ArgumentError("cannot convert $(eltype(xs)) to $T")) | ||
if isbitstype(T) && T===Q | ||
reinterpret(T, xs) | ||
else | ||
Q[x for x in xs] | ||
end | ||
end | ||
==# | ||
end | ||
# Resolve ambiguity | ||
#Base.convert(::Type{Array{Any,1}}, xs::Array{Any,1}) = xs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know why this is ambiguous, and the answer would probably go a long way to explaining why this breaks so much |
||
|
||
|
||
# Comparison operators | ||
==(::Missing, ::Missing) = missing | ||
|
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.