0

I have something like this:

trait Color {
  def myname: String = ""
}

trait White extends Color {
  override def myname = super.myname + " white "
}

trait Green extends Color {
  override def myname = super.myname + " green "
}

trait Yellow extends Color {
  override def myname = super.myname + " yellow "
}

object TestA {

  val traitnames: List[String] = List("White", "Green", "Yellow")

  def main(args: Array[String]) {

    class Colors extends White with Green with Yellow
    val c = new Colors

    println(c.myname)

  }
}

Is there a way how I can use the names from the List traitnames to mix traits in a class, let say, Colors - like is shown in the main method here - instead of writing the mixin statement manually?

Edit

Can I use annotations in someway around the traits to call through them later, to do the mixin?

Valerin
  • 465
  • 3
  • 19
  • 1
    Can you clarify - are you trying to dynamically mix in the the traits at runtime or trying to dynamically find out which traits are present? – Mike Curry Mar 26 '15 at 13:18
  • I do not want to write `class Colors extends White with Green with Yellow`. If we are using the word `dynamically` for the same meaning then yes I want to have something like `class Core extends T with U with V` and `T, U, V` are already implemented traits that I can mix dynamiclly through getting/calling by their names in the List. Trait implementations can be different, here I just gave an example. – Valerin Mar 26 '15 at 13:32
  • 1
    I don't think you can just mix in traits at runtime like that according to some arbitrary list. – Michael Zajac Mar 26 '15 at 13:36
  • @m-z, I wrote a comment here. If it's not possible to mix them dinamically (as I have explained), then do I need to dig more in macros? – Valerin Mar 26 '15 at 13:36

0 Answers0