Redundant type parameters


#1

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?


Could Julia have implicit type parameters?
#2

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:


#3

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?


#4

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


#5

You don’t necessarily need where

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

#6

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.


#7

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


#8

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.


#9

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

#10

No, this will be really bad for dispatch. The missing feature is https://github.com/JuliaLang/julia/issues/15791