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

Using to_dimension for dimension specific methods #37516

Open
Tokazama opened this issue Sep 10, 2020 · 0 comments
Open

Using to_dimension for dimension specific methods #37516

Tokazama opened this issue Sep 10, 2020 · 0 comments

Comments

@Tokazama
Copy link
Contributor

I think it would be useful if we had a formally defined to_dimension method that did something like:

@propagate_inbounds function to_dimension(A::AbstractArray{T,N}, d::Integer) where {T,N}
    @boundscheck if d < 1
        error("BoundsError: attempt to access $(typeof(A)) at dimension $d")
    end
    return Int(d)
end

@propagate_inbounds function to_dimension(A::AbstractArray{T,N}, dims::Tuple{Vararg{<:Integer,L}}) where {T,N,L}
    return ntuple(i -> to_dimension(A, getfield(dims, i)), Val(L))
end

This would provide more useful error messages and provide another useful hook into the array interface. For example, methods that have a dimension argument could easily be made type stable with the following:

@propagate_inbounds function to_dimension(A::AbstractArray, ::Val{D}) where {D}
    return to_dimension(A::AbstractArray, D)
end

# 
function dropdims(A; dims)
    d = to_dimension(A, dims)
    ....
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant