Col*_*ers 20
对于v0.7 +
使用fieldnames(x)其中x一个DataType.例如,使用fieldnames(Date),代替 fieldnames(today()),或使用fieldnames(typeof(today())).
这将返回Vector{Symbol}按顺序列出字段名称.
如果是字段名称myfield,则要使用该字段中的值getfield(x, :myfield)或快捷语法来检索该字段中的值x.myfield.
另一个有用的和相关的功能是dump(x).
在v0.7之前
使用fieldnames(x),where x是你感兴趣的复合类型的实例,或者a DataType.也就是说,fieldnames(today())并且fieldnames(Date)同样有效且具有相同的输出.
小智 5
假设目的是obj,
您可以使用以下代码片段获取其字段的所有信息:
T = typeof(obj)
for (name, typ) in zip(fieldnames(T), T.types)
println("type of the fieldname $name is $typ")
end
Run Code Online (Sandbox Code Playgroud)
这里,fieldnames(T)返回字段名称的向量,并T.types返回字段类型对应的向量。