Julia - 将子类型元素插入到超类型

时间:2017-04-30 04:14:30

标签: julia

我对Julia来说是全新的,

我有以下类层次结构,当我将子类型NonTerminal的实例插入到声明为超类型GrammarElement的数组中时,抛出异常

 GrammarElement
 |--> Terminal
 |--> Nonterminal

就这样

abstract GrammarElement

type Terminal <: GrammarElement
  name::String
end

type NonTerminal <: GrammarElement
  name::String
  rule::Array{GrammarElement,1}

  function NonTerminal(name::String)
     new(name,GrammarElement[])
   end
end

function and_with!(t,e)
  push!(t.rule,e)
end

但是当我使用and_with!函数时,测试会抛出异常

@testset "Test" begin
  r = NonTerminal("A")
  t=Terminal("B")
  and_with!(r,t)
end

失败(删除了邮件中的大量垃圾)

TestFirst: Error During Test
  Got an exception of type MethodError outside of a @test
  MethodError: Cannot `convert` an object of type Terminal to an object of type GrammarElement
  This may have arisen from a call to the constructor GrammarElement(...)
  • 为什么转换不可能?
  • 我该如何解决这个问题?
  • 声明父类型是否有用?

我知道julia主要用于科学计算而不是创建语法解析器,但是它应该完成这项工作并且我正在用该项目学习它

1 个答案:

答案 0 :(得分:2)

这似乎是一个问题,因为旧的编译代码仍在范围内(请参阅注释)。解决这个问题的最简单方法就是重启朱莉娅。

相关问题