There are a few things you can do:
- use the Vector and Matrix typealiases
- the definition of those also shows how to do this in general:
typealias Vector{T} Array{T,1}
If it is just a one-off thing for any type:
type A{T,S}
end
test{T}(a::A{T,1}) = -2
test{T}(a::A{T,2}) = 7
test(A{Int,2}()) # ==7
I.e. T is not used, except as place holder.
If you actually want to do a computation then you can use the type
vars. Your example:
testMatrix{T,N}(x::Array{T,N}) = println("It is $N-dimensional.")
julia> testMatrix(rand(2,2,3))
It is 3-dimensional.
On Sat, 2016-02-13 at 08:38, Po Choi <[email protected]> wrote:
> Can I do something like this?
> testMatrix(x::Array{1}) = println("It is 1-dimensional.")
> testMatrix(x::Array{2}) = println("It is 2-dimensional.")
>
> The above does not work because Array{1} == Array{1, N}
>
> One solution is
> testMatrixAgain{T}(x::Array{T, 1}) = println("It is 1-dimensional.")
> testMatrixAgain{T}(x::Array{T, 2}) = println("It is 2-dimensional.")
>
> However, the parametric type is actually not used. Is there any way that I
> don't need the parametric type?