Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia - Inserting subtype element to array of supertype

Tags:

julia

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(...)
  • Why isn't that conversion possible?
  • How can I solve this?
  • Is it event useful to declare the parent type?

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

like image 995
shakram02 Avatar asked Oct 17 '25 01:10

shakram02


1 Answers

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.

like image 154
Chris Rackauckas Avatar answered Oct 18 '25 22:10

Chris Rackauckas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!