Skip to content

Latest commit

 

History

History
58 lines (47 loc) · 1.39 KB

match-syntax.md

File metadata and controls

58 lines (47 loc) · 1.39 KB
title type num previous-page next-page
Match Expressions
section
63
/scala3/reference/changed-features/overload-resolution
/scala3/reference/changed-features/vararg-splices

The syntactical precedence of match expressions has been changed. match is still a keyword, but it is used like an alphabetical operator. This has several consequences:

  1. match expressions can be chained:

    xs match {
      case Nil => "empty"
      case _   => "nonempty"
    } match {
      case "empty"    => 0
      case "nonempty" => 1
    }

    (or, dropping the optional braces)

    xs match
      case Nil => "empty"
      case _   => "nonempty"
    match
      case "empty" => 0
      case "nonempty" => 1
  2. match may follow a period:

    if xs.match
      case Nil => false
      case _   => true
    then "nonempty"
    else "empty"
  3. The scrutinee of a match expression must be an InfixExpr. Previously the scrutinee could be followed by a type ascription : T, but this is no longer supported. So x : T match { ... } now has to be written (x: T) match { ... }.

Syntax

The new syntax of match expressions is as follows.

InfixExpr    ::=  ...
               |  InfixExpr MatchClause
SimpleExpr   ::=  ...
               |  SimpleExpr ‘.’ MatchClause
MatchClause  ::=  ‘match’ ‘{’ CaseClauses ‘}’