The following code compiles and runs in Scala 2:
trait SomeTrait {
val myName: String
}
class SomeClass() extends SomeTrait {
override val myName: String = "Billy Banana"
}
val someClass = new SomeClass()
println(someClass.myName)
val extendedTrait = new SomeTrait {
override val myName: String = "Amy Apples"
def printName(): Unit = println(myName)
}
extendedTrait.printName()
However in Scala 3, there is a compilation error at this line:
extendedTrait.printName()
What in the Scala 3 docs references this change?
Scala 2 used to infer structural types by default, Scala 3 does not.
val foo = new {
def bar: Unit = ???
}
// Scala 2:
// foo: AnyRef { def bar: Unit }
// Scala 3:
// foo: AnyRef
You'd have to explicitly make it a structural type:
val extendedTrait: SomeTrait {
def printName(): Unit
} = new SomeTrait {
override val myName: String = "Amy Apples"
def printName(): Unit = println(myName)
}