Redundant type parameters

From time to time I want to define a new type and have to introduce some redundant type parameters to do so.
For example I could want arrays that live in coordinate systems. In 0.5 I have to do something like

immutable CoordinateArray{A, C, T, N} <: AbstractArray{T, N}
    coordinates::A
    CoordinateArray(v) = new(CoordinateArray{T,N}(v))
end

However the T,N parameters lead to all kinds of annoyances. They are a hack, which obfuscates
displaying and every now and then the fact that CoordinateArray{A, C} is not a leaftype, forces
one to bookkeep them.

Is it possible to do something like

struct CoordinateArray{A <: AbstractArray{T, N}, C} <: AbstractArray{T, N}
    coordinates::A
end

in 0.6? If not is something planned in this direction?

2 Likes

Yes, that’s “triangular dispatch” (nesting of type information like A<: B{C} where C<:D where …, though here you’re not talking about dispatch so it’s not the correct term?), and it’s possible in v0.6.

You may want to read this part of the news:

Thanks, I am aware of triangular dispatch, but I was not able to do the above example (I both searched the manual and tried variations of the above).

julia> struct CoordinateArray{A <: AbstractArray{T, N}, C} <: AbstractArray{T, N}
           coordinates::A
       end
ERROR: UndefVarError: T not defined

Can you point me to the correct syntax?

I think you need to where T where N or something like that?

You don’t necessarily need where

julia> struct CoordinateArray{T,N,A<:AbstractArray{T,N},C} <: AbstractArray{T,N}
           coordinates::A
       end

I see, that’s the first bulletin:

Type parameter constraints can refer to previous parameters, e.g. type Foo{R<:Real, A<:AbstractArray{R}}. Can also be used in method definitions.

Ideally, it would be great to write the signature as

struct Foo{A<:AbstractArray{T,N}} <: AbstractArray{T,N} where T, N
end

But I don’t think this is possible (would love to be corrected).

3 Likes

Okay you got rid of the inner constructor, but CoordinateArray has still 4 explicit parameters {T,N,A,C} in your version. The goal was to get rid of N,T.

I don’t think this can be accomplished through the new type-system alone… the missing feature is probably computed subtyping: https://github.com/JuliaLang/julia/issues/8322

struct Foo{A<:AbstractArray} <: AbstractArray{eltype(A),ndims(A)}
end
3 Likes

No, this will be really bad for dispatch. The missing feature is Feature request: Type constructors as type parameters · Issue #15791 · JuliaLang/julia · GitHub

1 Like