Composition and inheritance: the Julian way

I’ve found the @forward macro from Lazy.jl quite useful for this purpose: https://github.com/MikeInnes/Lazy.jl/blob/76dab7d30863e8375fc96b2444fdb2ead369bf4c/src/macros.jl#L248-L271

Essentially, you can define:

struct Person
  name::String
end

name(p::Person) = person.name

struct Citizen
  person::Person 
  nationality::String
end

@forward Citizen.person name

which will automatically make name(c::Citizen) = name(c.person)

17 Likes