I want to be able to reference a list that contains subtypes and extract elements from this list and carry them implicitly. Example:
scala> sealed trait Person { def id: String } defined trait Person scala> case class Employee(id: String, name: String) extends Person defined class Employee scala> case class Student(id: String, name: String, age: Int) extends Person defined class Student scala> val x: List[Person] = List(Employee("1", "Jon"), Student("2", "Jack", 23)) x: List[Person] = List(Employee(1,Jon), Student(2,Jack,23)) scala> x(0).name <console>:14: error: value name is not a member of Person x(0).name ^
I know that x(0).asInstanceOf[Employee].name , but I was hoping there was a more elegant way with types. Thanks in advance.
source share