我想创建一个复合类型,其中包含一个字典作为其命名字段之一。但是明显的语法不起作用。我敢肯定有一些我不明白的基本原理。下面是一个例子:
type myType
x::Dict()
end
Julia 说:
type: myType: in type definition, expected Type{T<:Top}, got Dict{Any,Any}
这意味着,我猜,字典不是 Any
的任何命名字段都必须如此。但我不知道如何告诉它我的意思。我需要一个作为字典的命名字段。内部构造函数将初始化字典。
最佳答案
类型和实例之间的语法存在细微差别。 Dict()
实例化一个字典,而 Dict
本身就是类型。定义复合类型时,字段定义的形式需要为 symbol::Type
.
该错误消息有点令人困惑。它实际上是这样说的:
in type definition, expected something with the type
Type{T<:Top}
, got an instance of typeDict{Any,Any}
.In other words, it expected something like
Dict
, which is aType{Dict}
, but instead gotDict()
, which is aDict{Any,Any}
.
你想要的语法是
x::Dict
.
关于dictionary - Julia 中的复合类型 : Dictionaries as a named field?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486349/