# Redundant type parameters

**URL:** <https://discourse.julialang.org/t/redundant-type-parameters/2251>\
**Category:** Internals & Design\
**Created:** [February 23, 2017, 3:06pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251 "2017-02-23T15:06:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [February 23, 2017, 3:06pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/1 "2017-02-23T15:06:19Z")

</div>

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

```julia
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

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

```

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

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 23, 2017, 3:53pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/2 "2017-02-23T15:53:26Z")

</div>

> [@jw3126](#):
>
> in 0.6? If not is something planned in this direction?

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:

> <https://github.com/JuliaLang/julia/blob/master/NEWS.md#new-language-features>

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [February 23, 2017, 4:41pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/3 "2017-02-23T16:41:23Z")

</div>

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
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?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 23, 2017, 4:43pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/4 "2017-02-23T16:43:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 23, 2017, 5:06pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/5 "2017-02-23T17:06:24Z")

</div>

> [@jw3126](#):
>
> struct CoordinateArray{A \<: AbstractArray{T, N}, C} \<: AbstractArray{T, N}  
> coordinates::A  
> end

You don’t necessarily need where

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

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 23, 2017, 5:11pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/6 "2017-02-23T17:11:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MikeInnes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikeinnes/32/3656_2.png) [@MikeInnes](https://discourse.julialang.org/u/MikeInnes)\
**Post date:** [February 23, 2017, 5:18pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/7 "2017-02-23T17:18:31Z")

</div>

Ideally, it would be great to write the signature as

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

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [February 23, 2017, 5:24pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/8 "2017-02-23T17:24:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 23, 2017, 5:24pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/9 "2017-02-23T17:24:57Z")

</div>

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](https://github.com/JuliaLang/julia/issues/8322)

```julia
struct Foo{A<:AbstractArray} <: AbstractArray{eltype(A),ndims(A)}
end

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 23, 2017, 5:36pm UTC](https://discourse.julialang.org/t/redundant-type-parameters/2251/10 "2017-02-23T17:36:21Z")

</div>

> [@mbauman](#):
>
> struct Foo{A\<:AbstractArray} \<: AbstractArray{eltype(A),ndims(A)}

No, this will be really bad for dispatch. The missing feature is [Feature request: Type constructors as type parameters · Issue #15791 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15791)
