Julia: Making space for a swankier similar

Created on 12 Mar 2018  Â·  2Comments  Â·  Source: JuliaLang/julia

A major task I'd love to accomplish in 1.x is a redesign of similar. This has been discussed in oh-so-many issues (primarily #18161, but also #25107), and I don't want to rehash those discussions all that much. The similar function currently has lots of methods on it, with three "chains" of fallbacks:

┌similar(::AbstractArray)
├similar(::AbstractArray, ::Union{Integer,AbstractUnitRange}...)
├similar(::AbstractArray, ::Tuple)
├similar(::AbstractArray, ::Type, ::Union{Integer,AbstractUnitRange}...)
╘similar(::Array, ::Type, ::Tuple) = Array(…)
┌similar(::Type{T}, ::Union{Integer,AbstractUnitRange}...)
╘similar(::Type{T}, ::Tuple) = T(…)
┌similar(f, ::Union{Integer,AbstractUnitRange}...)
╘similar(f, ::Tuple) = f(…)

(Where ::Array is a stand-in for any object that similar supports).

I'd like to bifurcate this into a value-domain and a type-domain, wherein the type-domain methods become the canonical endpoints. In order to make space for that, though, I believe we'll need to deprecate the second two pairs of chains. Those guys are there to support offset arrays. I believe similar(f, …) can be deprecated in favor of just putting methods on f. similar(Type{<:Array}, ...) is messier, though, since it feels wrong to have Array(undef, 2:3, 2:3) return something that's not an Array.

arrays deprecation speculative

Most helpful comment

I'm not a fan of the methods with function arguments. For example, from the docstring:

    similar(dims->zeros(Int, dims), axes(A))

should just be written as zeros(Int, length.(axes(A))).

All 2 comments

Being able to call similar on a type rather than an instance would also be useful to support promotion for array concatenation in 1.x, see https://github.com/JuliaLang/julia/pull/20815.

I'm not a fan of the methods with function arguments. For example, from the docstring:

    similar(dims->zeros(Int, dims), axes(A))

should just be written as zeros(Int, length.(axes(A))).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omus picture omus  Â·  3Comments

manor picture manor  Â·  3Comments

wilburtownsend picture wilburtownsend  Â·  3Comments

i-apellaniz picture i-apellaniz  Â·  3Comments

helgee picture helgee  Â·  3Comments