타입 경계 또는 =가 기대돼요

타입 경계 또는 =가 기대돼요 (E095: Expected Type Bound Or Equals)

타입 파라미터나 타입 멤버 정의에서 = · >: · <: 가 기대되는데 그 외의 다른 것을 발견하면 이 에러가 나요. 타입 파라미터와 추상 타입은 타입 경계로 제약할 수 있어요.

출처: Scala 3 Reference

본문

타입 파라미터 또는 타입 멤버 정의의 문법이 잘못되어 = · >: · <: 를 기대했는데 다른 것을 발견하면 이 에러가 발생해요.

타입 파라미터와 추상 타입은 타입 경계(type bound)로 제약할 수 있어요.

  • = — 타입 별칭(type alias)에 사용
  • <: — 상한 경계(upper type bound, 하위 타입 제약)
  • >: — 하한 경계(lower type bound, 상위 타입 제약)

예시 (Example)

class Container:
  type MyType @

type MyType 뒤에 올바른 경계 기호가 아니라 @가 왔네요.

에러 (Error)

-- [E095] Syntax Error: example.scala:2:14 -------------------------------------
2 |  type MyType @
  |              ^
  |              =, >:, or <: expected, but '@' found
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Type parameters and abstract types may be constrained by a type bound.
  | Such type bounds limit the concrete values of the type variables and possibly
  | reveal more information about the members of such types.
  |
  | A lower type bound B >: A expresses that the type variable B
  | refers to a supertype of type A.
  |
  | An upper type bound T <: A declares that type variable T
  | refers to a subtype of type A.
   -----------------------------------------------------------------------------

해결 방법 (Solution)

타입 별칭이라면 =를 사용해요.

// Use = for type alias
class Container:
  type MyType = Int

상한 경계가 필요하면 <:를 써요.

// Use <: for upper bound
class Container:
  type MyType <: AnyVal

하한 경계는 >:로 표현해요.

// Use >: for lower bound
class Container:
  type MyType >: Nothing