# Multiple dispatch constructor not multiple dispatching?

**URL:** <https://discourse.julialang.org/t/multiple-dispatch-constructor-not-multiple-dispatching/14685>\
**Category:** General Usage\
**Created:** [September 7, 2018, 9:35pm UTC](https://discourse.julialang.org/t/multiple-dispatch-constructor-not-multiple-dispatching/14685 "2018-09-07T21:35:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)\
**Post date:** [September 7, 2018, 9:35pm UTC](https://discourse.julialang.org/t/multiple-dispatch-constructor-not-multiple-dispatching/14685/1 "2018-09-07T21:35:42Z")

</div>

I’m trying to do multiple dispatch magic with various ways of representing a geometric transformation matrix. I have:

```
using StaticArrays

struct Quaternion{T<:Real}
    s::T
    v1::T
    v2::T
    v3::T
end

mutable struct transform
    pos::SVector{3, Cdouble}
    rot::SVector{4, Cdouble}
end

transform(pos::SVector{3, Cdouble}, q::Quaternion) = transform(pos, SVector{4, Cdouble}([q.v1, q.v2, q.v3, q.s]))

```

I can successfully create a transform struct by doing the normal thing:

```
T = transform([0., 0., 0.], [0., 0., 0., 1.])

```

but if I try

```
Q = Quaternion(1., 0., 0., 0.)
T = transform([0., 0., 0.], Q)

```

I get the error

```
ERROR: MethodError: Cannot `convert` an object of type Orientation.Quaternion{Float64} to an object of type StaticArrays.SArray{Tuple{4},Float64,1,4}
This may have arisen from a call to the constructor StaticArrays.SArray{Tuple{4},Float64,1,4}(...),
since type constructors fall back to convert methods.

```

…which makes me think that Julia is not attempting to call the constructor I defined as taking a Quaternion type, but is instead trying to convert() Q directly to an SVector.

This should be easy, which makes me think I am missing something obvious. But for the life of me I can’t see it.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 7, 2018, 9:39pm UTC](https://discourse.julialang.org/t/multiple-dispatch-constructor-not-multiple-dispatching/14685/2 "2018-09-07T21:39:13Z")

</div>

Please provide enough code so that the error can be reproduced by just copy pasting things into a REPL.

Ok, all that is needed was `using StaticArrays`.

Edit:

Your `transform` function needs a `SVector` as the first argument and you give it a normal `Vector`. Use e.g:

```julia
 T = transform(@SVector([0., 0., 0.]), Q)

```

Right now it will fail to match your outer constructor and try use the inner one (which tries to convert arugments).

---

<div class="post-metadata">

**Author:** ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)\
**Post date:** [September 7, 2018, 9:42pm UTC](https://discourse.julialang.org/t/multiple-dispatch-constructor-not-multiple-dispatching/14685/3 "2018-09-07T21:42:21Z")

</div>

“Please provide enough code so that the error can be reproduced by just copy pasting things into a REPL.”

Yes, I have modified the code to make that explicit. Thanks.

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [September 7, 2018, 9:43pm UTC](https://discourse.julialang.org/t/multiple-dispatch-constructor-not-multiple-dispatching/14685/4 "2018-09-07T21:43:40Z")

</div>

I think the issue is the first argument of your constructor. What you are trying to pass in is a `Vector`, but you wrote your constructor to require an `SVector`. Try annotating that first type with `AbstractVector` instead.

---

<div class="post-metadata">

**Author:** ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)\
**Post date:** [September 7, 2018, 9:44pm UTC](https://discourse.julialang.org/t/multiple-dispatch-constructor-not-multiple-dispatching/14685/5 "2018-09-07T21:44:12Z")

</div>

Aha! Yes, that’s it. Thanks.
