E013: 객체는 셀프 타입을 가질 수 없음
E013: 객체는 셀프 타입을 가질 수 없음 (Object May Not Have Self Type)
이 에러는 object 정의에 셀프 타입(self type) 주석이 포함될 때 발생해요. Scala에서 객체는 셀프 타입을 가질 수 없어요.
본문
셀프 타입은 클래스나 트레이트가 다른 트레이트와 믹스인(mix-in)되어야 함을 선언할 때 사용해요. 하지만 객체는 싱글턴 인스턴스라서 정의된 후에는 다른 트레이트로 확장되거나 믹스인될 수 없어요. 그래서 객체에게 셀프 타입은 의미가 없죠.
Example
trait Foo
object Test { self: Foo => }
Error
-- [E013] Syntax Error: example.scala:3:14 -------------------------------------
3 |object Test { self: Foo => }
| ^^^^^^^^^^^^
| objects must not have a self type
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| objects must not have a self type:
|
| Consider these alternative solutions:
| - Create a trait or a class instead of an object
| - Let the object extend a trait containing the self type:
|
| object Test extends Foo
-----------------------------------------------------------------------------
Solution
// Create a trait or class instead of an object
trait Foo
class Test extends Foo
// Or let the object extend the trait directly
trait Foo
object Test extends Foo
// Or use a class with a self type if you need the pattern
trait Foo
trait Bar
class Test { self: Foo & Bar =>
// ...
}
더 알아보기
- 셀프 타입과 의존성 주입 패턴에 대한 내용은 "Self Types" 관련 문서를 참고하세요.