Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia 迭代元组数据类型中的类型_Julia - Fatal编程技术网

Julia 迭代元组数据类型中的类型

Julia 迭代元组数据类型中的类型,julia,Julia,有没有一种方法可以迭代Tuple{…}DataType中包含的类型?例如,如果我有类型Tuple{String,Int},我希望能够使用值(Tuple{String,Int})之类的东西来返回String和Int的迭代器,如下所示: julia> collect(values(Tuple{String, Int})) 2-element Array{DataType,1}: String Int64 但这当然不起作用: julia> values(Tuple{String, I

有没有一种方法可以迭代
Tuple{…}
DataType中包含的类型?例如,如果我有类型
Tuple{String,Int}
,我希望能够使用
值(Tuple{String,Int})
之类的东西来返回
String
Int
的迭代器,如下所示:

julia> collect(values(Tuple{String, Int}))
2-element Array{DataType,1}:
 String
 Int64
但这当然不起作用:

julia> values(Tuple{String, Int})
Tuple{String,Int64}

julia> collect(values(Tuple{String, Int}))
ERROR: MethodError: no method matching length(::Type{Tuple{String,Int64}})
Closest candidates are:
  length(::Core.SimpleVector) at essentials.jl:596
  length(::Base.MethodList) at reflection.jl:852
  length(::Core.MethodTable) at reflection.jl:938
  ...
Stacktrace:
 [1] _similar_for(::UnitRange{Int64}, ::Type{Any}, ::Type{T} where T, ::Base.HasLength) at ./array.jl:576
 [2] _collect(::UnitRange{Int64}, ::Type{T} where T, ::Base.HasEltype, ::Base.HasLength) at ./array.jl:609
 [3] collect(::Type{T} where T) at ./array.jl:603
 [4] top-level scope at REPL[30]:1

我更喜欢一个不涉及挖掘
数据类型
内部的解决方案

元组类型只是
数据类型
。在其上运行的所有操作都必须涉及
数据类型
——您正在寻找类型为
DataType->[DataType]
的函数。一个可能的答案是
Tuple{String,Int}.parameters
。至少在1.3中,
Core.Compiler
还包含

datatype_fieldtypes(x::DataType) = ccall(:jl_get_fieldtypes, Any, (Any,), x)
不过,这只不过是内部的,没有记录在案的。两者都会产生一个
核心.SimpleVector

但后来我想起元组部分可以被视为索引和字段。因此,
字段类型
可能是您最喜欢的:

julia> fieldtypes(Tuple{Int, String})
(Int64, String)
但是,其他方法的优点是,您可以将它们用于任何参数化类型。这通常在生成的函数中很方便