# Convert and parametric types

**URL:** <https://discourse.julialang.org/t/convert-and-parametric-types/1936>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 6, 2017, 11:40am UTC](https://discourse.julialang.org/t/convert-and-parametric-types/1936 "2017-02-06T11:40:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 6, 2017, 11:40am UTC](https://discourse.julialang.org/t/convert-and-parametric-types/1936/1 "2017-02-06T11:40:44Z")

</div>

I have a type `MyObject{S}` which basically wraps a vector of `S <: AbstractArray{T,N}` elements, with a few bells and whistles.

I would like to define a method for `convert` which basically concatenates these, giving an `Array{T,N+1}`, but I am unsure about the type signature. Should it be

```julia
convert{S}(::Type{Array}, x::MyObject{S})

```

or

```julia
convert{S,T,N}(::Type{Array{T,N}), x::MyObject{S}) 

```

or something else? [The manual](http://docs.julialang.org/en/latest/manual/conversion-and-promotion.html#Conversion-1) does not talk about this.

---

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [February 6, 2017, 12:04pm UTC](https://discourse.julialang.org/t/convert-and-parametric-types/1936/2 "2017-02-06T12:04:45Z")

</div>

bear in mind that Type{Super} is not a supertype of Type{Sub}. therefore in your example, the first version will only kick in if you explicitly call `convert(Array, ...)`. the second version will be called for any actual dense array types `Array{T,N}`. you could also do

`convert{S, T <: Array}(::Type{T}, x::MyObject(S)) = ...`

this will be called for all dense arrays, and you can decide what to in the body do based on T. i don’t think you will ever be able to enforce the N+1 criteria though.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 6, 2017, 1:49pm UTC](https://discourse.julialang.org/t/convert-and-parametric-types/1936/3 "2017-02-06T13:49:23Z")

</div>

Thanks — I am aware of how dispatch works, the question was more about idomatic/recommended style. Eg with `Array`, the caller does not have to fiddle with specifying types, which is great. But perhaps not all callers of `convert` are users (eg automatic conversions)? Should one have two methods, one with and one without type parameters, the former calling the latter?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 7, 2017, 9:18pm UTC](https://discourse.julialang.org/t/convert-and-parametric-types/1936/4 "2017-02-07T21:18:33Z")

</div>

Ideally, you should define both of them, and even a third one with `T` but not `N`. That’s the only way to allow users to call `convert` with zero, one of two type parameters depending on their needs. I find this quite tedious, but that’s how it works (at least currently).
