E156: 이 정의에는 그 수식어를 쓸 수 없어요

E156: 이 정의에는 그 수식어를 쓸 수 없어요 (Modifier Not Allowed For Definition)

정의에 맞지 않는 수식어를 사용할 때 발생하는 에러예요. 정의의 종류(class, object, 메서드 등)에 따라 쓸 수 있는 수식어의 집합이 달라요.

출처: Scala 3 Reference

본문

흔한 잘못된 조합은 다음이 있어요.

  • opaque는 타입 별칭(type alias)에만 쓸 수 있는데, 메서드 정의에 붙이는 경우

Example

object Test {
  opaque def example: Int = 42
}

Error

-- [E156] Syntax Error: example.scala:2:13 -------------------------------------
2 |  opaque def example: Int = 42
  |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |  Modifier opaque is not allowed for this definition

Solution

object Test {
  // opaque is only valid for type aliases
  opaque type PositiveInt = Int

  // Regular method definition
  def example: Int = 42
}
// abstract is not allowed on object - use trait or abstract class instead
abstract class Base {
  def example: Int
}

더 알아보기

  • opaque 타입과 각 정의 종류에 허용되는 수식어에 대한 자세한 내용은 Scala 3 Reference를 참고하세요.