Match types and inline

inline asks the compiler to put the body of a definition at the call site instead of calling it, which is what lets a parameter be a constant, a branch disappear and a loop over types be unrolled. A match type goes one step further and computes a type from a type: it takes a type argument and reduces to one of its cases, at compile time, with no value involved.

inline and inline match

inline val Answer = 42 is a constant of the call site, and power takes its exponent as an inline parameter, so the recursion is a match on a constant and the compiler writes the multiplications out:

inline.scala

inline val Answer = 42 inline def power(x: Double, inline n: Int): Double = inline n match case 0 => 1.0 case _ => x * power(x, n - 1) @main def inlined(): Unit = println(Answer) println(power(2.0, 10)) println(power(3.0, 3))

scala-cli run --server=false inline.scala

42
1024.0
27.0

power(2.0, 10) is ten multiplications written by the compiler, and an inline n match needs n to be a constant to choose a branch at all: a value read from a file cannot be passed there. The same mechanism is behind inline if and the logging macros of a library, which disappear from a release build.

A type computed from a type

Elem[X] reads: if X is a String the element is a Char, if it is an array or a list the element is the type inside. The three values below are declared with the type the match produced, and the compiler checks each one against it:

match_types.scala

type Elem[X] = X match case String => Char case Array[t] => t case List[t] => t @main def match_types(): Unit = val letter: Elem[String] = 'x' val number: Elem[Array[Int]] = 1 val word: Elem[List[String]] = "s" println(letter) println(number) println(word)

scala-cli run --server=false match_types.scala

x
1
s

A match type is how a library gives a method a result type that depends on its argument, as ++ gives back the collection it was given. It reduces only when the argument is known: Elem[String] becomes Char at compile time and is never a type that exists at run time.

When no case matches

A type that matches none of the cases leaves the match type unreduced, and a value cannot be declared with a type that is still a match expression:

unmatched.scala

type Elem[X] = X match case String => Char case Array[t] => t case List[t] => t @main def unmatched(): Unit = val number: Elem[Int] = 1 println(number)

mkdir -p out
scalac -color:never -d out unmatched.scala 2>&1; echo "exit status: $?"

-- [E007] Type Mismatch Error: unmatched.scala:7:26 ----------------------------
7 | val number: Elem[Int] = 1
  | ^
  | Found: (1 : Int)
  | Required: Elem[Int]
  |
  | Note: a match type could not be fully reduced:
  |
  | trying to reduce Elem[Int]
  | failed since selector Int
  | matches none of the cases
  |
  | case String => Char
  | case Array[t] => t
  | case List[t] => t
  |
  | longer explanation available when compiling with `-explain`
-- [E184] Type Warning: unmatched.scala:7:6 ------------------------------------
7 | val number: Elem[Int] = 1
  | ^
  | Match type reduction failed since selector Int
  | matches none of the cases
  |
  | case String => Char
  | case Array[t] => t
  | case List[t] => t
1 warning found
1 error found
exit status: 1

The error names the two failures: the type could not be fully reduced, and the selector Int matches none of the cases. The compiler also warns that the reduction failed, which is the message to read first when a match type misbehaves. The fix is a case for the type that was passed, or a default case case _ => X at the end of the match, which makes the type total.

Related

Article author: Arthur Isaev