当我尝试在另一个结构中包含一个结构数组时,Julia抛出错误。
ERROR: LoadError: syntax: "grid = Cell[]" inside type definition is reserved
这是我要运行的测试代码。
struct Cell
color::Int64
location::Int64
Cell(color::Int64,location::Int64) = new(color,location)
Cell()=new(0,0)
end
struct Grid
dimensions::Int64
timestep::Int64
grid=Cell[]
courseGrain=Cell[]
Grid(dimensions::Int64,timestep::Int64,grid_cell::Cell,courseGrain_cell::Cell) =
new(dimensions,timestep,push!(grid,grid_cell),push!(courseGrain,courseGrain_cell))
end
答案 0 :(得分:0)
当前不允许在字段声明中定义默认字段值。参见https://github.com/JuliaLang/julia/issues/10146
要实现所需的功能,请将grid
和courseGrain
定义为类型为Cell
(即Array{Cell, 1}
或等效为Vector{Cell}
)的一维数组,并处理构造函数中的默认情况。
struct Grid
dimensions::Int64
timestep::Int64
grid::Vector{Cell}
courseGrain::Vector{Cell}
Grid(dimensions::Int64,timestep::Int64,grid_cell::Cell,courseGrain_cell::Cell) =
new(dimensions,timestep,[grid],[courseGrain_cell])
end
如果您希望构造函数之一创建一个空的grid
或courseGrain
,则可以在对Cell[]
的调用中编写Vector{Cell}(undef, 0)
或new
。