简体   繁体   English

如何在 julia 中创建递归类型别名?

[英]How do I create a recursive typealias in julia?

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?这种类型typealias通常是如何完成的?

(I am in julia 0.4) (我在茱莉亚 0.4)

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注意:我只能在 036 中尝试此操作,而不能在 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM