0

I have something like this:

trait AO extends A {
  private var link: String = "AO"
  //...
}
trait AR extends A {
  private var link: String = "AR"
  //...
}    

object AO extends AO with Atr {
  atr += link
}

object AR extends AR with Atr {
  atr += link  
}

trait Atr {
  import scala.collection.mutable._
  val atr: ListBuffer[String] = ListBuffer()
  def echo() = {
    for(f <- atr) println("attribute: " + f)
  }
}

object TestF {
  def main(args: Array[String]) {
    AR.echo //not what I want!
    AO.echo //not what I want!
    echo() //doesn't show nothing...
  }
}

How I can add such attributes in a ListBuffer, let say like atr, and read them all at once like in the third line on TestF?

Valerin
  • 465
  • 3
  • 19
  • First, I made them `private` because I am going to use them only by that specific `class/trait Atr` (it compiles but it's not mandatory to be private). Second, I wanted to put an attribute of all the `traits` in a list and to be able to read it before mixing those traits in a class or object in runtime. – Valerin Feb 04 '15 at 15:21
  • I am compiling :D, and it compiles because the private members can be seen between an abstract class/trait and their companion objects. – Valerin Feb 04 '15 at 16:20
  • Do you want that third echo to display `attribute: AR, attribute: AO`? – ggovan Feb 04 '15 at 16:49
  • @ggovan, yes like a list..and instead of attributes string, I can pass the names of the Types directly with `val link = this.getClass().getSimpleName()` – Valerin Feb 04 '15 at 19:05

0 Answers0