Luca
Luca

Reputation: 10996

Abstract type in Julia and member variables

I am very confused as to whether abstract types in Julia can have member variables (like virtual classes in C++). I am sure the docs mention it but I cannot find it!

I tried something like:

abstract AbstractR
  source
end

And now I have a concrete implementation as:

type RR <: AbstractR
end

function loadSource(reg::RR, x::AbstractString)
  println("Hello")
end

However, when I try to load the module, I have:

ERROR: LoadError: LoadError: LoadError: UndefVarError: source not defined

So is it that abstract type is just a type name and nothing else? Also, why did the language designers decide to not support member variables (if that is indeed the case). Also, I do not then really see the point of having an abstract type anyway...

EDIT

Module file

module TestProj
export AbstractR
export RR
include("generic.jl")
include("rr.jl")
end

generic.jl

abstract AbstractR
  source
end

rr.jl

type RR <: AbstractR
end

function loadSource(reg::RR, x::AbstractString)
  println("Hello")
end

Upvotes: 2

Views: 842

Answers (2)

Alexander Morley
Alexander Morley

Reputation: 4181

At least the way I understand it is that abstract types are intended to serve as "nodes" and as such are not themselves instantiateable (sp?). As such they allow you organise concrete types into hierarchies & provide an easy way for methods to dispatch on any of a group of concrete types.

I think the docs explain this here.

(With this the error msg should make sense as well?)

Upvotes: 6

miguel raz
miguel raz

Reputation: 526

You are loading a module then? Perhaps you must import stuff first. Could you post the entirety of the code?

Upvotes: 0

Related Questions