I realise this is a newbie question, but I think it’s sort of central to the whole Julia ethos. I really like the idea of moving away from object-orientation’s emphasis on single dispatch, and that lattices of types perform a lot of the work of subclassing. And I quite see it makes sense that you can’t derive from a concrete type. And yet I’ve come to a point where I don’t really understand how to proceed.
I want to create a type State
that is essentially a Vector{Complex}
, but I want to give it extra constraints like maybe the sum of all components must always be equal to 1. Normally, in the object-riented world, I’d do this by subclassing and providing constructors and operations that preserve the constraint. But of course, in Julia, I can’t derive from Vector{Complex}
. I could say State
is a struct
that contains a Vector{Complex}
, but then I’d have to relay all existing Vector functionality from the field to the struct
. The simplest idea seems to be to declare them equal:
State = Vector{Complex}
But then how do I create State
s? Julia won’t let me declare a State()
constructor. I could create a method state()
that creates State
s that fulfil the necessary constraints, but that seems a cheat. As far as I can see, I’m missing something essential about the Julia model here, but I don’t know what it is, and I’d love for someone to tell me.
Thanks.