How do I create a recursive typealias in julia?

458 Views Asked by At

I would like to create a nested tuple type, that can hold itself, or the particular type it contains.

So I thought:

typealias NestedTuple{T} Tuple{Union(T,NestedTuple{T}),Union(T,NestedTuple{T})}

However this comes up with an error

LoadError: UndefVarError: NestedTuple not defined

How is this kind of typealias normally done?

(I am in julia 0.4)

1

There are 1 best solutions below

0
On BEST ANSWER

Dose this work for what you are doing?

typealias NestedTuple0{N,T} Tuple{Union(T,N),Union(T,N)}
typealias NestedTuple{T} NestedTuple0{NestedTuple0{T},T}

Note: I am only able to try this in 036, not 04


An exmple of use:

function rnt(p)
    np = 0.95*p
    a=rand() <p ? rnt(np) : 1
    b=rand() <p ? rnt(np) : 2
    (a,b)
end

x=rnt(1.0)

typeof(x)<:NestedTuple{Int64} #returns true