Convert and parametric types



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

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


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

or something else? The manual does not talk about this.


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.


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?


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