E104: Trait Is Expected
E104: Trait Is Expected (트레이트가 필요한 자리)
트레이트(trait)만 허용되는 자리에 클래스(class)를 사용했을 때 나오는 에러예요.
본문
Scala에서 클래스에 무언가를 섞어 넣을 때(mixin)는 with 키워드로 트레이트만 사용할 수 있어요. 클래스는 이런 식으로 섞어 넣을 수 없죠.
예시
class A
class B
val example = new A with B
에러 메시지
-- [E104] Syntax Error: example.scala:4:25 -------------------------------------
4 |val example = new A with B
| ^
| class B is not a trait
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Only traits can be mixed into classes using a with keyword.
| Consider the following example:
|
| class A
| class B
|
| val a = new A with B // will fail with a compile error - class B is not a trait
|
| The example mentioned above would fail because B is not a trait.
| But if you make B a trait it will be compiled without any errors:
|
| class A
| trait B
|
| val a = new A with B // compiles normally
-----------------------------------------------------------------------------
해결 방법
클래스를 트레이트로 바꾸거나, with 대신 extends를 사용하면 돼요.
// Change the class to a trait
class A
trait B
val example = new A with B
// Or use extends instead of with
class A
class B extends A
val example = new B()