On Tue, 2014-11-18 at 22:06, David Smith <david.sm...@gmail.com> wrote: > I would like to have a type such as this: > type foo{T,M,N} > x::Array{T,M} > y::Array{T,N} > end > > Where T and M can be anything, but N = M + 1. I know I can't write > > type foo{T,M} > x::Array{T,M} > y::Array{T,M+1} > end > > So I wrote the following naive code, but I get an error: > > julia> function foo{T,M,N}(x::Array{T,M}, y::Array{T,N}) > @assert (M + 1) == N > return foo(x,y) > end > foo{T,M,N} (constructor with 1 method) > > julia> foo(rand(3),rand(3,3)) > ERROR: stack overflow > in foo at none:2 > in foo at none:3 (repeats 79999 times)
This stackoverflow happens because you call foo again and again... Anyway, this seems to work: immutable Foo{T,M,N} # probably want an immutable here, so nobody swaps # out the arrays behind your back. x::Array{T,M} y::Array{T,N} function Foo(x,y) @assert (M + 1) == N new(x,y) end end Foo{T, M, N}(x::Array{T,M}, y::Array{T,N}) = Foo{T, M, N}(x,y) Foo(rand(3),rand(3,2)) Foo(rand(3),rand(3,2,3)) # errors Note that the outer constructor is needed, see http://docs.julialang.org/en/latest/manual/constructors/#parametric-constructors (Note, types are by convention capitalized)