Hi Zenna, there has been a long discussion in https://github.com/JuliaLang/julia/issues/4935 on the subject. Please have a look at te whole thread as the thinking has changed quite a bit.
In short you can also just assume that an asbtract type has certain field. Then you would only have to write the data function one time. If you are looking for overloading getfield, here is the relevant issue: https://github.com/JuliaLang/julia/issues/1974 Cheers, Tobias Am Dienstag, 8. Juli 2014 09:39:08 UTC+2 schrieb Zenna Tavares: > > > > 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? >
