MethodError(f = typeof(Core.Compiler.fieldindex()

时间:2019-08-16 12:44:44

标签: julia

我有以下代码段,尝试通过TreeStochastic类型扩展类型系统。

module my_mod



abstract type ScalarVariate <: Real end
abstract type ArrayVariate{N} <: DenseArray{Float64, N} end
abstract type TreeVariate <: Any end

const AbstractVariate = Union{ScalarVariate, ArrayVariate, TreeVariate}

#################### My Types ####################

mutable struct Node
    name::String
    data::Array{Float64,2}
end

mutable struct ScalarLogical <: ScalarVariate
  value::Float64
  symbol::Symbol
end

mutable struct ArrayLogical{N} <: ArrayVariate{N}
  value::Array{Float64, N}
  symbol::Symbol
end

mutable struct ScalarStochastic <: ScalarVariate
  value::Float64
  symbol::Symbol
end

mutable struct ArrayStochastic{N} <: ArrayVariate{N}
  value::Array{Float64, N}
  symbol::Symbol
end

mutable struct TreeStochastic <: TreeVariate
    value::Node
    symbol::Symbol
end

const AbstractLogical = Union{ScalarLogical, ArrayLogical}
const AbstractStochastic = Union{ScalarStochastic, ArrayStochastic, TreeStochastic}
const AbstractDependent = Union{AbstractLogical, AbstractStochastic}


Base.size(v::AbstractVariate) = size(v.value)

function Stochastic(d::Integer)
  if d == 0
    value = Float64(NaN)
    s = ScalarStochastic(value, :nothing)
  else
    value = Array{Float64}(undef, fill(0,d)...)
    s = ArrayStochastic(value, :nothing)
  end
  s
end

function Logical(d::Integer)
  if d == 0
    value = Float64(NaN)
    s = ScalarLogical(value, :nothing)
  else
    value = Array{Float64}(undef, fill(0,d)...)
    s = ArrayLogical(value, :nothing)
  end
  s
end

function Stochastic(d::AbstractString)
  TreeStochastic(Node("",zeros(Float64,(1,2))), :nothing)
end

function Model(; nodes...)
  nodedict = Dict{Symbol, Any}()
  for (key, value) in nodes
    isa(value, AbstractDependent) || throw(ArgumentError("nodes are not all Dependent types"))
    node = deepcopy(value)
    node.symbol = key
    nodedict[key] = node
  end
  nodedict
end

end

调用模块并执行功能Model后,我收到一条内部错误消息:

  

内部错误:运行时遇到意外错误:   MethodError(f = typeof(Core.Compiler.fieldindex)(),args =(my_mod.ArrayLogical {N},其中N,:symbol,false),world = 0x0000000000000eb9)

该函数的调用方式如下:

include("f1.jl")
using .my_mod
my_mod.Model(y=my_mod.Stochastic(1),x=my_mod.Logical(2), z=my_mod.Stochastic("t"))

如果我在没有z=my_mod.Stochastic("t")的情况下运行该函数,那么一切都会按预期进行。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

根据Julia团队,此错误应使用属于Julia 1.2的Pull请求#31670进行修复。因此,使用Julia 1.2或更高版本应该可以解决该问题。