# Specialize to vector with specified dimension

**URL:** <https://discourse.julialang.org/t/specialize-to-vector-with-specified-dimension/50711>\
**Category:** New to Julia\
**Created:** [November 24, 2020, 8:25pm UTC](https://discourse.julialang.org/t/specialize-to-vector-with-specified-dimension/50711 "2020-11-24T20:25:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 24, 2020, 8:25pm UTC](https://discourse.julialang.org/t/specialize-to-vector-with-specified-dimension/50711/1 "2020-11-24T20:25:10Z")

</div>

In my move to Julia one thing that I go back and forth is on the type annotation of function parameters. One particular case is when a function can accept vectors of any type of numbers, but those vectors must be of a specific dimension. A simple example would be a function that computes the distance between points in three-dimensions:\*

```julia
function dist(x,y)
   d = zero(eltype(x))
   for i in 1:3
      d += (x[i]-y[i])^2
   end
   sqrt(d)
end

```

\*Of course, in this case, one can write easily the function that does the same for every dimension. But this is not the point, because in my codes I am only dealing with points in 3D space and thus I am referring to every function of my codes, some for which that is simple, some for which that would be cumbersome.

So, my question is: What is the best practice for the type annotation of parameters? Leave it blank (as it is there)?

State at least that they should be vectors:

```julia
dist(x::AbstractVector,y::AbstractVector}

```

Abstract vectors of a given type? `AbstractVector{Float64}`, although that would limit the function somehow (the fact that `AbstractVector{Real}` does not work is somewhat unintuitive - I understand why is that, but it is unintuitive noneless; edit: should be `AbstractVector{T} where T<:Real` to work, wright?).

And, finally, it seems there is no way to declare, at least, that it must be an abstract vector of dimension 3, is it (except if specialized to static arrays, for instance). One thing I already used is

```julia
dist(x :: T, y :: T) where T <: MyVectorType

```

where `MyVectorType` is a constant global inside my package which defines the type of data I am using (`SVector{3,Float64}`, for example). But than I took it back to most clean forms.

More generally it could be

```julia
dist(x :: AbstractVector{T1},y :: AbstractVector{T1}) where {T1, T2 <: Real}

```

but seems too bloated… and still does not define the length of the vectors, which is themost important.

What are the recommendations here?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 24, 2020, 8:53pm UTC](https://discourse.julialang.org/t/specialize-to-vector-with-specified-dimension/50711/2 "2020-11-24T20:53:59Z")

</div>

common patter is to define short-hand for (union of) type(s) you use a lot:

```julia
const AV = AbstractArray

function dist(x::AV{<:Real}, y::AV{<:Real})

```

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [November 24, 2020, 9:17pm UTC](https://discourse.julialang.org/t/specialize-to-vector-with-specified-dimension/50711/3 "2020-11-24T21:17:49Z")

</div>

To define the length in the type, take a look at StaticArrays.jl

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 24, 2020, 10:00pm UTC](https://discourse.julialang.org/t/specialize-to-vector-with-specified-dimension/50711/4 "2020-11-24T22:00:54Z")

</div>

I did both things. I am still using static arrays in general. My doubts are on the function type annotations, and when they help or become just bloat.

The short hand types I took back after a while, because I felt that the code was less clear at a function level.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [November 24, 2020, 10:24pm UTC](https://discourse.julialang.org/t/specialize-to-vector-with-specified-dimension/50711/5 "2020-11-24T22:24:10Z")

</div>

@oxinabox wrote an opinionated piece on this which you might find useful: [JuliaLang Antipatterns](https://www.oxinabox.net/2020/04/19/Julia-Antipatterns.html) (check the part on Over-constraining argument types)
