Same koan as above, but we are pattern matching a list with only one item!
To obtain the second element you can expand on the pattern.
To obtain the second element you can expand on the pattern. Where x
is the first element, y
is the second element, and xs
is the rest:
To pattern match against List
, you can also establish a pattern match if you know the exact number of elements in a List
:
To pattern match against a List
, the list can be broken out into parts, in this case the head x
and the tail xs
.
To pattern match against a List
, the list can be broken out into parts, in this case the head x
and the tail xs
. Since the case doesn't terminate in Nil
, xs
is interpreted as the rest of the list:
Pattern matching can match complex expressions:
A backquote can be used to refer to a stable variable in scope to create a case statement.
A backquote can be used to refer to a stable variable in scope to create a case statement. This prevents what is called "Variable Shadowing"
Scala has a built-in general pattern matching mechanism.
Scala has a built-in general pattern matching mechanism. It allows to match on any sort of data with a first-match policy. Here is a small example which shows how to match against an integer value:
object MatchTest1 extends App { def matchTest(x: Int): String = x match { case 1 => "one" case 2 => "two" case _ => "many" } println(matchTest(3)) }
The block with the case
statements defines a function which maps integers to strings. The match
keyword provides a convenient way of applying a function (like the pattern matching function above) to an object.
Scala's pattern matching statement is most useful for matching on algebraic types expressed via case classes
.
Scala also allows the definition of patterns independently of case classes, using unapply
methods in extractor objects.
Pattern matching returns something:
Pattern matching can return complex somethings:
A backquote can be used to refer to a method parameter as a stable variable to create a case statement:
Pattern matching can substitute parts of expressions:
Pattern matching can wildcard parts of expressions:
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
Please use 'an [Exception] should be thrownBy { ... }' syntax instead
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.