-
-
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
RFC: Introduce promotion mechanism for concatenation #20815
base: master
Are you sure you want to change the base?
Conversation
Could you show an example of how this can be used to address JuliaStats/NullableArrays.jl#167? |
Haven't tested yet, but something like this should work: promote_type_cat(::Type{S}, ::Type{T}) where {S<:AbstractArray, T<:NullableArray} =
NullableArray{promote_type(eltype(S), eltype(T))}
promote_type_cat(::Type{S}, ::Type{T}) where {S<:NullableArray, T<:AbstractArray} =
NullableArray{promote_type(eltype(S), eltype(T))}
similar{T,A<:NullableArray{T},N}(::Type{A}, dims::Dims{N}) = NullableArray{T, N}(dims) |
This is very interesting @nalimilan. From your example, it seems that The final thing is - if we need a specific promotion mechanism for concatenation - do we in general need a promotion mechanism for |
That's just for simplicity for a first approach. I've listed it in the TODOs (when I mentioned
That's an interesting question. Currently the fallbacks for |
For triage: This clearly won't make the feature freeze, but in the perspective of introducing this mechanism in the 1.x series I wonder whether we shouldn't change the rule that the type of the first array is used. Indeed, it would not fit very well in a general promotion mechanism: it's as if Maybe it would be more appropriate to always return an |
That might be a bit less breaking but not by much. Triage would rather punt on this for 1.0. |
FWIW, this would be really useful for operator-overloading-based AD systems like Flux and Knet currently have. It's current hard for us to intercept calls like |
Interestingly, dplyr is developing a system for concatenation which is very similar to this PR: https://stat.ethz.ch/pipermail/r-devel/2018-August/076550.html For this to work, they even have to introduce a promotion mechanism (which doesn't exist in R). |
This used to make a lot of references to design issues with the SparseArrays package (JuliaLang/julia#2326 / JuliaLang/julia#20815), which result in a non-sensical dispatch arrangement, and contribute to a slow loading experience do to the nonsense Unions that must be checked by subtyping.
This used to make a lot of references to design issues with the SparseArrays package (JuliaLang/julia#2326 / JuliaLang/julia#20815), which result in a non-sensical dispatch arrangement, and contribute to a slow loading experience do to the nonsense Unions that must be checked by subtyping. (cherry picked from commit 5a922fadfef083b83d983ac84f49a4460baa42ab) (cherry picked from commit 8fd5f279c8c81a8e59dec3852235c7dd5c88b143)
This is a first attempt to explore a fix for #2326, i.e. stop relying on the type of the first array to decide the type of the result of
cat
functions. The idea is to introduce a promotion system similar topromote
but for concatenation.Remarks/issues to resolve:
promote
mechanism, but it turns out we need a separate one. For example, concatenatingDiagonal
matrices should give a sparse matrix, not aDiagonal
matrix. IOW, promoting a series of identical types for concatenation is not the same as promoting them for conversion (which is the goal ofpromote
).promote_eltype
) and the container type. That could allow e.g. returning aBitArray
when concatenating anArray{Bool} with a
BitArray`. The number of dimensions isn't involved since it depends on the kind of concatenation performed in a way that would make the system too complex.Array
objects unless a promotion rule has been defined. This could be changed so that concatenations involving only arrays of a custom type default to that type (though I'm not sure it's easy to write using dispatch). Thoughts? This is also an issue due to the next bullet.similar(::Type{A{T}}, dims)
methods being defined for all types which define promotion rules, i.e. which want to opt-out from concatenation returning standardArray
objects. AFAICT this is required since there is no generic method to take an array type and change just its element type (cf. this discussion aboutfixate_eltype
); indeed that's not possible e.g. forBitArray
. I think that method could be made one of the fundamental requirements ofAbstractArray
, possibly redefining othersimilar
methods to call it by default: with an instance, you also have the type, but the contrary doesn't work.promote_rule
to avoid the need to define methods twice for each ordering of arguments.typed_*cat
lowering toeltyped_*cat
as it was simpler to prevent confusion. A less disruptive solution would be to preserve the existing lowering, and use another name for the new methods which take the array type instead of the element type.promote_type_cat
name should probably be improved.