一个UnionAll和它的任意实例化之间如何互相推导出

就看single parametric的情况即可.

struct Ty{T} end 

concrete = Ty{Int}

是否有这样一个函数some_fn能够得到如下关系

Ty === some_fn(concrete) # => true 

反过来的话, 鉴于没有类型约束时可选concrete type无穷多, 而Ty.var本身也以归纳方式描述了forall T match [Ty.var.lb] < T < [Ty.var.ub]. Ty{T}, 所以从UnionAll到Concrete这个方向就很清楚了.

所以只要有从Concrete导出UnionAll的方法就好了.

是想要类似typejointypeintersect这种函数么?

julia> typeintersect(Ty, concrete)
Ty{Int64}

julia> typejoin(Ty, concrete)
Ty
1 个赞

thx! typejoin works well. 要是有它的码源就更好了…

在REPL里@less typejoin(Ty, concrete)就可以查看,或者@edit在编辑器中打开,如果配置了JULIA_EDITOR环境变量。

1 个赞

再次感谢.
请问是否有直接从concrete到Ty的函数或者宏?

这个应该没有,type相关的操作很少,文档: https://docs.julialang.org/en/latest/base/base/#Properties-of-Types-1

2 个赞
julia> struct Ty{T} end

julia> concrete = Ty{Int}
Ty{Int64}

julia> concrete.name
Ty

这个方向我说了是平凡的啊。如何从Ty{Int64} → Ty{T}才是关键。。

julia> eval(Symbol(concrete.name)) === Ty{T} where T
true

大概?

请注意,我现在只有一个对象t, 其值是Ty{Int64}
我想解构它拿到其中的Ty和Int64, 其中拿到Int64是简单的,拿parameters属性就行。主要是拿Ty

base_ty = get_base(t)

请原谅我的愚钝。。。并没明白你的意思。。。 base_ty的值不是 Ty{T} where T 吗?

julia> (Ty{T} where T) === Ty
true
julia> struct Ty{T} end

julia> t = Ty{Int}
Ty{Int64}

julia> Base.@pure get_base(t) = t.name.wrapper.body
get_base (generic function with 1 method)

julia> get_base(t)
Ty{T}
julia> Base.@pure get_base2(t) = t.name.wrapper
get_base2 (generic function with 1 method)

julia> get_base2(t)
Ty

julia> typeof(ans)
UnionAll

emmm,我尽力了。GG

2 个赞

cool(凑齐8字