Match

org.scalajs.ir.Trees$.Match
sealed case class Match(selector: Tree, cases: List[(List[MatchableLiteral], Tree)], default: Tree)(tpe: Type)(implicit pos: Position) extends Tree

A break-free switch (without fallthrough behavior).

Unlike a JavaScript switch, it can be used in expression position. It supports alternatives explicitly (hence the List[MatchableLiteral] in cases), whereas in a switch one would use the fallthrough behavior to implement alternatives. (This is not a pattern matching construct like in Scala.)

The selector must be either an int (IntType) or a java.lang.String. The cases can be any MatchableLiteral, even if they do not make sense for the type of the selecter (they simply will never match).

Because +0.0 === -0.0 in JavaScript, and because those semantics are used in a JS switch, we have to prevent the selector from ever being -0.0. Otherwise, it would be matched by a case IntLiteral(0). At the same time, we must allow at least int and java.lang.String to support all switchable matches from Scala. Since the latter two have no common super type that does not allow -0.0, we really have to special-case those two types.

This is also why we restrict MatchableLiterals to IntLiteral, StringLiteral and Null. Allowing more cases would only make IR checking more complicated, without bringing any added value.

Attributes

Graph
Supertypes
trait Product
trait Equals
class Tree
class IRNode
class Object
trait Matchable
class Any

Members list

Concise view

Value members

Inherited methods

Attributes

Inherited from:
Product

Attributes

Inherited from:
Product

Attributes

Inherited from:
IRNode

Concrete fields

val tpe: Type

Implicits

Implicits

implicit val pos: Position