Skip to content
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

Making space for a swankier similar #26433

Closed
mbauman opened this issue Mar 12, 2018 · 2 comments
Closed

Making space for a swankier similar #26433

mbauman opened this issue Mar 12, 2018 · 2 comments
Labels
arrays [a, r, r, a, y, s] deprecation This change introduces or involves a deprecation speculative Whether the change will be implemented is speculative

Comments

@mbauman
Copy link
Member

mbauman commented Mar 12, 2018

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.

@mbauman mbauman added speculative Whether the change will be implemented is speculative deprecation This change introduces or involves a deprecation arrays [a, r, r, a, y, s] labels Mar 12, 2018
@nalimilan
Copy link
Member

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 #20815.

@JeffBezanson
Copy link
Member

JeffBezanson commented Mar 13, 2018

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))).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] deprecation This change introduces or involves a deprecation speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

3 participants