Reputation: 5449
could someone please help me understand the error here? I think I understand anonymous class construction with traits in Scala. However, when I try to apply more than one trait I get an error expecting ";" or essential end of statement. The same problem seems to apply if I declare a class this way as well (with multiple traits that require anonymous implementation lines of code ? Line Test 3 fails below. Thank you.
class TestTraits
trait A {def x:Int}
trait B {def y:Int}
object TestTraits {
def main(args: Array[String]): Unit = {
val test1 = new TestTraits with A {def x=22} //OK
val test2 = new TestTraits with B {def y=33} //OK
val test3 = new TestTraits with A {def x=22} with B {def y=33} //Errors: - ';' expected but 'with'
}
}
Upvotes: 2
Views: 179
Reputation: 134280
Your syntax is invalid:
val test3 = new TestTraits with A with B {def x=22; def y=33}
A class definition can only have one body and what you are declaring is an anonymous class.
Upvotes: 5