shakram02
shakram02

Reputation: 11886

Julia - Inserting subtype element to array of supertype

I'm totally new to Julia,

I have the following class hierarchy, when I insert an instance of the subtype NonTerminal into an array that's declared of the supertype GrammarElement, an exception is thrown

 GrammarElement
 |--> Terminal
 |--> Nonterminal

As such

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

But when I use the and_with! function the test throws an exception

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

Failing on ( removed lots of garbage in the message )

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(...)

I'm aware that julia is mainly for scientific computing and not creating grammar parsers, but it should do the job and I'm learning it with that project

Upvotes: 4

Views: 340

Answers (1)

Chris Rackauckas
Chris Rackauckas

Reputation: 19162

This seemed to be a problem due to having older compiled code still in the scope (see the comments). The easiest way to handle this is just to restart Julia.

Upvotes: 3

Related Questions