선언만 있고 정의되지 않은 멤버는 클래스만 가능
선언만 있고 정의되지 않은 멤버는 클래스만 가능 (Only Classes Can Have Declared But Undefined Members)
객체처럼 클래스나 트레이트가 아닌 곳에 구현 없이 선언만 된 추상 멤버가 있을 때 나오는 에러예요.
본문
클래스나 트레이트가 아닌 맥락(예: 객체)에 구현 없이 선언만 된 추상 멤버가 있으면 이 에러가 발생해요.
추상 멤버는 클래스와 트레이트만 가질 수 있어요. 객체는 구체적인 싱글턴 인스턴스이므로 모든 멤버가 완전히 구현되어야 해요. 변수(variable)도 정의되려면 초기화가 필요해요.
예시
object Example:
def abstractMethod: Int
에러 메시지
-- [E067] Syntax Error: example.scala:2:6 --------------------------------------
2 | def abstractMethod: Int
| ^
|Declaration of method abstractMethod not allowed here: only classes can have declared but undefined members
해결 방법
// Provide an implementation in objects
object Example:
def concreteMethod: Int = 42
// Or use a trait or abstract class for abstract members
trait Example:
def abstractMethod: Int
object ConcreteExample extends Example:
def abstractMethod: Int = 42
더 알아보기
- 객체에서는 구현을 채워 넣고, 추상 멤버가 필요하다면 트레이트나 추상 클래스를 쓰면 돼요.