问题描述
假设我有一个类型为 NullableArray{Int64}
的对象——我怎么知道元素的类型是 Int64
(嗯,Nullable{Int64}
) 而不实际访问任何元素(即,可以在对象为空时完成).有没有一种通用的方法可以从 Type{SubType}
类型的对象中找出 SubType
是什么?
Suppose I have an object which has a type like NullableArray{Int64}
-- how do I know that the elements have type Int64
(well, Nullable{Int64}
) without actually accessing any element (ie, it can be done when the object is empty). Is there a general way to figure out what SubType
is from an object of type Type{SubType}
?
哎呀,为了扩展下面接受的答案,我意识到我正在做 eltype(x) 但需要为 Nullables 的示例做 eltype(eltype(x)).
Whoops, to expand on the accepted answer below I realized I was doing eltype(x) but needed to do eltype(eltype(x)) for the example of Nullables.
推荐答案
你可以使用 eltype
assert(eltype(collect(1:3)) == Int)
这篇关于获取容器中对象的类型,如 Array 或 NullableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!