# Is there a typealias for Arrays of dimension less than N?

**URL:** <https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094>\
**Category:** New to Julia\
**Created:** [June 5, 2017, 8:18pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094 "2017-06-05T20:18:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 5, 2017, 8:18pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/1 "2017-06-05T20:18:13Z")

</div>

I am trying to compactly represent Union{Array{T,1}, Array{T,2}, Array{T,3}, … Array{T,N}}, where N is known. Is there a way to do that?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 5, 2017, 8:36pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/2 "2017-06-05T20:36:01Z")

</div>

One could write a macro for creating such an expression

```julia
macro UnionForArraysOfRankLessThanN(T,N)
    args = [:(Array{$T,$n}) for n ∈ 1:N]
    esc(Expr(:curly, :Union, args...))
end

```

Though it’s probably best to ask yourself some serious questions about whether this is really the right way to do things before you implement something like this.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 5, 2017, 9:34pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/3 "2017-06-05T21:34:15Z")

</div>

Interesting. I will see if I can avoid it without having to make too many types.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 5, 2017, 11:00pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/4 "2017-06-05T23:00:48Z")

</div>

Probably what you want to use is simply `Array{T}` which is an array with element type `T` of any rank. I’m having a very hard time imagining any situation in which it would help you to define the union you described.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 5, 2017, 11:25pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/5 "2017-06-05T23:25:46Z")

</div>

I know about Array{T}, not quite as limiting as what I was hoping for, but then I can set a constraint on N in the constructor/function and throw an error if the given array exceeds the threshold value. My use case is representing scalar and vector data defined over 2D and 3D structured meshes, represented by the same type. Taking all combinations of 1, 2 or 3 components of the data field, and 2 or 3 mesh dimensions, this could be thrown at me as a 2, 3 or 4 dimensional array. The geometry itself can be 2 or 3 dimensional arrays.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 5, 2017, 11:50pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/6 "2017-06-05T23:50:19Z")

</div>

Hm, I’m rather doubtful that there can be any performance advantage of using the type system to check for this rather than doing `@assert 2 ≤ N ≤ 6` as there is some significant overhead in dispatching on complicated types, but if I’m wrong hopefully someone more knowledgeable will come along and correct me.

So, my suggestion is that you do

```julia
function _core_functionality(A::Array)
    # what you are actually doing lives here
    # you may have multiple such functions for arrays of different rank
end

function userfacingfunction{T,N}(A::Array{T,N})
    # stuff that may change the rank of the array goes here
    @assert 2 ≤ N ≤ 6
    _core_functionality(A)
end

```

The function boundary ensures that the compiler knows how to specialize for arrays of different rank.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 6, 2017, 6:41am UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/7 "2017-06-06T06:41:53Z")

</div>

@mohamed82008, by any chance, are you working on VTK-like meshes?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 6, 2017, 1:52pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/8 "2017-06-06T13:52:05Z")

</div>

Yep! I am working on the type system for VTK mesh data, file readers and writers for most geometric file formats, and converters between VTK mesh types, e.g. structured → unstructured.

---

<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:** [June 6, 2017, 2:05pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/9 "2017-06-06T14:05:19Z")

</div>

No macros required:

```julia
julia> ArrayN(T, N) = Union{[Array{T,n} for n = 1:N]...}

julia> ArrayN(Int, 5)
Union{Array{Int64,1}, Array{Int64,2}, Array{Int64,3}, Array{Int64,4}, Array{Int64,5}}

julia> ArrayN(T, 5) where T
Union{Array{T,1}, Array{T,2}, Array{T,3}, Array{T,4}, Array{T,5}} where T

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 6, 2017, 2:15pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/10 "2017-06-06T14:15:06Z")

</div>

@mohamed82008 where is this development happening, is it open source? It may be really useful for a package that I am working on: [Announcement: GeoStats.jl](https://discourse.julialang.org/t/announcement-geostats-jl/4098) I don’t want to reinvent the wheel if you have something working already.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 6, 2017, 2:23pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/11 "2017-06-06T14:23:47Z")

</div>

I am still working on it, but it’s kind of an early stage of development, probably 20%. I got the data type heirarchy and some basic functions pretty much finalized. Still working on the readers and converters, then come the writing, testing and debugging.

Integration with GeometryTypes is another task down the road probably after release.

I can share the type hierarchy if you want, because that is probably final.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 6, 2017, 2:38pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/12 "2017-06-06T14:38:17Z")

</div>

Already shared some of my code in another topic. Feel free to use or change it.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 6, 2017, 2:44pm UTC](https://discourse.julialang.org/t/is-there-a-typealias-for-arrays-of-dimension-less-than-n/4094/13 "2017-06-06T14:44:16Z")

</div>

So this is not in a package? Bad news.
