Inheritance in Julia

Hello, I´m new in Julia language. I´m thinking to write a framework that allows me to work with evolutionary algorithms. I come from Java language and the first idea that I had was to create a type hierarchy. I´m using Julia 0.6.

Just for illustrating what is my problem. I have the following type that represents an individual of the population.

struct Individual{T}
  chromosome::Vector{T}

  #Inner constructor
  Individual{T}(ngenes::UInt) where T<:Number =  new{T}(Vector{T}(ngenes))
end

Suppose that I want to create a composite type that inherits of Individual, for instance, an individual having a binary or real representation, in such a way that in the new composite type I would not need to declare the chromosome field. For instance

struct BinaryIndividual <: Individual

  #Inner constructor
  BinaryIndividual(ngenes::UInt) = new{Bool}(ngenes)

end

I have tried several manners to do this, but I always got the following error:

invalid subtyping in definition of BinaryIndividual.

The only way that I have found is declaring Individual as an abstract type, but in this case, Julia does not allow me to put any field into the abstract type, therefore I must to put the chromosome field into each composite type that inherits of Individual, and this is not actually a good OOP practice.

My question is: Is there a way to do this in Julia?,

Thanks.

–Oscar

3 Likes