Skip to content

Some quiet type cleanup #124

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ A[ClosedInterval(0.,.3), [:a, :c]] # select an interval and two columns
```

"""
struct AxisArray{T,N,D,Ax} <: AbstractArray{T,N}
struct AxisArray{T,N,D<:AbstractArray{T,N},Ax<:NTuple{N,Axis}} <: AbstractArray{T,N}
Copy link
Member

@mbauman mbauman Sep 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long time ago there were some very subtle and surprising dispatch issues in doing this. Looks like at least the minimal case I reported still exists:

julia> struct Foo{A<:Real}; end
       f(x::Foo{A}) where A = 1 # Method sorting doesn't take into account `A <: Real`
       f(x::Foo, y...) = 2      # Implicitly assumes `A <: Real`
f (generic function with 2 methods)

julia> f(Foo{Int}())
2

Not sure if any other methods still depend on that behavior.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the ambiguity no longer occurs but that still happens.

Do you know if there is a Julia issue for this behaviour?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data::D # D <:AbstractArray, enforced in constructor to avoid dispatch bugs (https://github.com/JuliaLang/julia/issues/6383)
axes::Ax # Ax<:NTuple{N, Axis}, but with specialized Axis{...} types
AxisArray{T,N,D,Ax}(data::AbstractArray{T,N}, axs::Tuple{Vararg{Axis,N}}) where {T,N,D,Ax} = new{T,N,D,Ax}(data, axs)
AxisArray{T,N,D,Ax}(data::AbstractArray{T,N}, axs::NTuple{N,Axis}) where {T,N,D,Ax} = new{T,N,D,Ax}(data, axs)
end

# Helper functions: Default axis names (if not provided)
Expand Down Expand Up @@ -302,9 +302,9 @@ reduced_indices(axs::Tuple{Vararg{Axis}}, region::Integer) =
reduced_indices0(axs::Tuple{Vararg{Axis}}, region::Integer) =
reduced_indices0(axs, (region,))

reduced_indices(axs::Tuple{Vararg{Axis,N}}, region::Dims) where {N} =
reduced_indices(axs::NTuple{N,Axis}, region::Dims) where {N} =
map((ax,d)->d∈region ? reduced_axis(ax) : ax, axs, ntuple(identity, Val{N}))
reduced_indices0(axs::Tuple{Vararg{Axis,N}}, region::Dims) where {N} =
reduced_indices0(axs::NTuple{N,Axis}, region::Dims) where {N} =
map((ax,d)->d∈region ? reduced_axis0(ax) : ax, axs, ntuple(identity, Val{N}))

@inline reduced_indices(axs::Tuple{Vararg{Axis}}, region::Type{<:Axis}) =
Expand Down Expand Up @@ -360,16 +360,16 @@ Base.transpose(A::AxisArray{T,1}) where {T} = AxisArray(transpose(A.data), Axis
Base.ctranspose(A::AxisArray{T,1}) where {T} = AxisArray(ctranspose(A.data), Axis{:transpose}(Base.OneTo(1)), A.axes[1])

Base.map!(f::F, A::AxisArray) where {F} = (map!(f, A.data); A)
Base.map(f, A::AxisArray) = AxisArray(map(f, A.data), A.axes...)
Base.map(f, A::AxisArray{T,N,D,Ax}) where {T,N,D,Ax} = AxisArray(map(f, A.data), A.axes...)

function Base.map!(f::F, dest::AxisArray{T,N,D,Ax}, As::AxisArray{T,N,D,Ax}...) where {F,T,N,D,Ax<:Tuple{Vararg{Axis}}}
function Base.map!(f::F, dest::AxisArray{T,N,D,Ax}, As::AxisArray{T,N,D,Ax}...) where {F,T,N,D,Ax}
matchingdims((dest, As...)) || error("All axes must be identically-valued")
data = map(a -> a.data, As)
map!(f, dest.data, data...)
return dest
end

function Base.map(f, As::AxisArray{T,N,D,Ax}...) where {T,N,D,Ax<:Tuple{Vararg{Axis}}}
function Base.map(f, As::AxisArray{T,N,D,Ax}...) where {T,N,D,Ax}
matchingdims(As) || error("All axes must be identically-valued")
data = map(a -> a.data, As)
return AxisArray(map(f, data...), As[1].axes...)
Expand Down