I can't understand why a syntax error occurs in this code:
"T.scala" file :
trait T
"A.scala" file :
class A
object A {
def apply() = new A()
}
"other.scala" file :
val a = new A() with T // ok
val b = A() with T // syntax error ';' or newline expected
Can I create a new class instance with inheriting without using the new
keyword?
I've not found anything about it.
A()
translates to A.apply()
which is a method that returns a new object and is not the same as new A()
.
You are essentially trying to do this (which will not work):
val b = A.apply()
val c = b with T
You can't use with
on an object that has already been created.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.