Workaround for traditional inheritance features in object-oriented languages

Another approach that has not yet been mentioned, but which is a very useful pattern:

struct Person{Job,PrivateLife,Quirks}
    name::String
    age::Int
    job::Job
    privatelife::PrivateLife
    otherfields::Quirks
end

age(person::Person) = person.age

const Employee = Person{Job,PrivateLife,Quirks} where Job <: CorporateJob
const MarriedPerson = Person{Job,PrivateLife,Quirks} where PrivateLife <: Married
...etc

In many cases where you have an “abstract base class” struct with a ton of fields, that you want to specialize to some other concrete types with additional behaviour, composition with generic components is often a better design.

EDIT: (Didn’t realize I was bumping a ten months old thread.)

10 Likes