问题描述
假设我有一个对象,其类型类似于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
you can use eltype
assert(eltype(collect(1:3)) == Int)
这篇关于获取容器(如Array或NullableArray)中的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!