E032: Illegal Start Of Simple Pattern — 패턴 시작이 올바르지 않아요

E032: Illegal Start Of Simple Pattern — 패턴 시작이 올바르지 않아요

패턴 매치 문맥에서 유효한 패턴을 시작할 수 없는 토큰을 컴파일러가 만났을 때 이 에러가 나와요.

출처: Scala 3 Reference

본문

간단한 패턴(simple pattern)은 여러 종류로 나눌 수 있어요.

  • 변수 패턴(Variable Patterns): case x => ... 또는 case _ => ... — 어떤 값이든 매치하고 변수 이름을 그 값에 묶어요. 와일드카드 패턴 _는 등장할 때마다 새로운 변수처럼 취급돼요.
  • 타입 패턴(Typed Patterns): case x: Int => ... 또는 case _: Int => ... — 지정된 타입과 매치되는 값을 매치하고 변수 이름을 그 값에 묶어요.
  • given 패턴(Given Patterns): case given ExecutionContext => ... — 지정된 타입과 매치되는 값을 매치하고 given 인스턴스를 그 값에 묶어요.
  • 리터럴 패턴(Literal Patterns): case 123 => ... 또는 case 'A' => ... — 지정된 리터럴과 같은 값을 매치해요.
  • 안정 식별자 패턴(Stable Identifier Patterns): 백틱을 사용해 변수에 대해 매치하는 방식이에요 — case \y` => ...`
  • 생성자 패턴(Constructor Patterns): case Person(name, age) => ... — 객체의 모든 필드를 변수 이름에 묶어요.
  • 튜플 패턴(Tuple Patterns): case (a, b) => ...
  • 패턴 시퀀스(Pattern Sequences): case List(first, second, rest*) => ...

예제 (Example)

def example(x: Any) = x match
  case => "none"

case 바로 뒤에 어떤 패턴도 오지 않았어요. 패턴이 기대되는 자리인데 토큰이 없죠.

오류 메시지 (Error)

-- [E032] Syntax Error: example.scala:2:7 --------------------------------------
2 |  case => "none"
  |       ^^
  |       pattern expected
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Simple patterns can be divided into several groups:
  | - Variable Patterns: case x => ... or case _ => ...
  |   It matches any value, and binds the variable name to that value.
  |   A special case is the wild-card pattern _ which is treated as if it was a fresh
  |   variable on each occurrence.
  |
  | - Typed Patterns: case x: Int => ... or case _: Int => ...
  |   This pattern matches any value matched by the specified type; it binds the variable
  |   name to that value.
  |
  | - Given Patterns: case given ExecutionContext => ...
  |   This pattern matches any value matched by the specified type; it binds a given
  |   instance with the same type to that value.
  |
  | - Literal Patterns: case 123 => ... or case 'A' => ...
  |   This type of pattern matches any value that is equal to the specified literal.
  |
  | - Stable Identifier Patterns:
  |
  |   def f(x: Int, y: Int) = x match
  |     case `y` => ...
  |
  |   the match succeeds only if the x argument and the y argument of f are equal.
  |
  | - Constructor Patterns:
  |
  |   case class Person(name: String, age: Int)
  |
  |   def test(p: Person) = p match
  |     case Person(name, age) => ...
  |
  |   The pattern binds all object's fields to the variable names (name and age, in this
  |   case).
  |
  | - Tuple Patterns:
  |
  |   def swap(tuple: (String, Int)): (Int, String) = tuple match
  |     case (text, number) => (number, text)
  |
  |   Calling:
  |
  |   swap(("Luftballons", 99))
  |
  |   would give (99, "Luftballons") as a result.
  |
  | - Pattern Sequences:
  |
  |   def getSecondValue(list: List[Int]): Int = list match
  |     case List(_, second, x*) => second
  |     case _ => 0
  |
  |   Calling:
  |
  |   getSecondValue(List(1, 10, 2))
  |
  |   would give 10 as a result.
  |   This pattern is possible because a companion object for the List class has a method
  |   with the following signature:
  |
  |   def unapplySeq[A](x: List[A]): Some[List[A]]
   -----------------------------------------------------------------------------

해결 방법 (Solution)

유효한 패턴을 사용하면 돼요. 타입 패턴, 변수 패턴, 생성자 패턴 등을 상황에 맞게 쓰면 되죠.

// Use a valid pattern - typed pattern
def example(x: Any) = x match
  case _: String => "string"
  case _ => "other"
// Use a variable pattern
def example(x: Any) = x match
  case y => s"value: $y"
// Use a constructor pattern
case class Person(name: String)

def example(x: Any) = x match
  case Person(name) => s"person: $name"
  case _ => "not a person"

더 알아보기 (Learn more)