Suppose I have an abstract class. I want to provide a kind of default
property of all sub-types of it.
I believe I can do this using a function, for example
abstract Machine
function data(m::Machine)
Int[]
end
type BigMachine <: Machine
data::Vector{Integer}
end
function data(m::BigMachine) m.data end
type SmallMachine <: Machine
end
data(BigMachine([1,2,3]))
data(SmallMachine())
This provides the intended default behavour. My belief is that this will
be more space efficient since I am not storing with each machine an empty
data vector.
Is there a penalty in lookup?
It's also not ideal, I did it his way because I cannot overload the
getfield function. The problems here are now that 1. I can't use the dot
notation to access a field 2. For each non-default machine type I have to
define a new accessor function. I suppose I could use a type-union, but it
seems unnecessarily complicated
Is there an idiomatic solution to this?