I recommend a parameterized module for this situation:
trait GenotypeSystem {
type Genotype <: GenotypeLike
trait GenotypeLike {
def makeRandom(): Genotype
def mutate(): Genotype
}
}
// Example implementation
object IntGenotypeSystem extends GenotypeSystem {
case class Genotype(x: Int) extends GenotypeLike {
def makeRandom() = copy(x = Random.nextInt(10))
def mutate(): Genotype = copy(x = x + Random.nextInt(3) - 1)
}
}
// Example abstract usage
def replicate(gs: GenotypeSystem)(g: gs.Genotype, n: Int): Seq[gs.Genotype] =
Seq.fill(n)(g.mutate())
This approach lends itself easily to future modification and extension, such as adding other types to the GenotypeSystem
.