클래스만 abstract일 수 있음

클래스만 abstract일 수 있음 (Only Classes Can Be Abstract)

메서드나 값처럼 클래스가 아닌 대상에 abstract 수식어를 붙였을 때 나오는 에러예요.

출처: Scala 3 Reference

본문

클래스가 아닌 대상(예: 메서드, 값)에 abstract 수식어를 사용하면 이 에러가 발생해요.

abstract 수식어는 클래스에만 쓸 수 있어요. 추상 멤버(메서드, 값)의 경우에는 구현을 그냥 생략하면 되는데, 트레이트나 추상 클래스 안에서 본문 없이 선언하면 자동으로 추상으로 취급돼요.

예시

trait Example:
  abstract def method: Int

에러 메시지

-- [E063] Syntax Error: example.scala:2:15 -------------------------------------
2 |  abstract def method: Int
  |               ^
  |abstract modifier can be used only for classes; it should be omitted for abstract members

해결 방법

// Simply omit abstract for member declarations
trait Example:
  def method: Int
// Use abstract only for class definitions
abstract class Example:
  def method: Int
  def concreteMethod: Int = 42

더 알아보기

  • 멤버 선언에서는 abstract를 빼고, 클래스 정의에서만 abstract를 쓰면 돼요.