I created this small Scala example for understand better traits.
trait Writer {
def write(value: Int): Unit = {
print("Writer")
}
}
trait Hdd extends Writer {
override def write(value: Int): Unit = {
print("Hdd")
}
}
trait File extends Writer {
override def write(value: Int): Unit = {
print("File")
}
}
class TestClass extends App {
(1) val myWriter = new Writer with Hdd // This line looks fine
(2) val myNewWriter = new Writer // This line fail
}
In my understanding, it's not possible to instantiate a Trait, and for this reason the line (2) is failing.
But for some reason that I'm not able to understand, the line (1) looks fine.
How this can be possible?