score:4
you have a scope problem. m
and n
are not visible outside the pattern matching case.
this works:
object helloworld extends app {
val a: any = 1
val x = a match{
case 2 => 22
case m: string =>
println(s"m = $m")
"aa"
case n: int =>
println(s"n = $n")
11
}
println(s"x = $x")
}
prints:
n = 1
x = 11
solution to the updated question::
object helloworld extends app {
var m: any = ()
var n: any = ()
val a: any = 1
val x = a match {
case 2 => 22
case m1: string =>
m = m1
"aa"
case n1: int =>
n = n1
11
}
println(s"x = $x")
println(s"m = $m n = $n")
}
prints:
x = 11
m = () n = 1
but this is not good scala style!
score:3
val m = ???
val n = ???
val x = a match {
case 2 => 22 //if a is 2 x=22
case m: string => "aa" //if a is a string create new variable m, x="aa"
case n: int => 11 //if a is an int create new variable n, x=11
}
//if m or n were created forget them, now out of scope
//only original (unchanged) m and n variables are visible
score:4
this wording is sloppy:
if the case keyword is followed by a variable name, then the match expression is assigned to that variable
the case
expression does not assign to an existing variable, it creates a new variable which is only available within the body of the case
.
it should read:
if the
case
keyword is followed by a variable name, then a variable with that name is created and the matching value is assigned to that variable. the scope of the variable is the scope of thecase
body.
Source: stackoverflow.com
Related Query
- Scala Variable Pattern: variable is not assigned
- Scala macros: string literal assigned to a variable does not match
- Why does pattern matching in Scala not work with variables?
- In scala pattern matching, what is suspicious shadowing by a variable pattern?
- Scala pattern matching with lowercase variable name
- Scala pattern matching keep saying "match is not exhaustive!"
- Scala pattern match is not exhaustive on nested case classes
- Scala implicit conversion not getting applied on Java argument pattern
- Scala pattern match and logical not
- Scala Play passing variable to view not working
- Missing parameter type for expanded function when not assigned to a variable
- Why does Scala not infer the type parameters when pattern matching with @
- variable binding to pattern in case clause in scala match expression
- unapply method of a case class is not used by the Scala compiler to do pattern matching, why is that?
- Value of a scala variable do not change while executing play Future query. Why?
- Scala Regex Pattern Match not able to get it to work
- Scala pattern match on ADTs may not warn on unreachable code
- Assigned variable not passed to a map function in Spark
- Scala pattern matching variable binding
- scala error illegal variable in pattern alternative on pattern matching case
- Scala pattern matching with disjunctions not working
- Pattern matching - value is not a member on a bound variable
- Why binding a variable to Exception types in a Scala pattern with pipes is inferred to type "Throwable"?
- scala - how to bind a variable name in a multiple pattern matching clause
- Why set can be assigned to the variable but not arguments
- scala pattern matching - variable
- Pattern matching not matching given variable
- pattern matching in scala works inside a def, otherwise does not
- Why does Regex pattern matching not work sometimes in Scala
- Scala null pointer exception cause by .toString on a variable which is not null
More Query from same tag
- Scala: How to use case keyword without match keyword?
- Scala huffman decoding
- In Scala 2.8, how to write (append) a line to a file? Should I use Java clesses or there are native Scala functions for this?
- Indent setting for scala new line dot notation in Intellij
- How to modify value type of a json via Circe
- How to use Scala XML with Apache Flink?
- What are Scala's future platform concerns people should be prepared for?
- Eclipse Scala interpreter (REPL) - proper use and debugging
- Is there a way to run a single benchmark with sbt-jmh?
- Where is synthetic sugar for generics with two type parameters (ie. that F[A,B] is equal to A F B) documented?
- How do I write recursive anonymous functions?
- Access variable from another class in Scala
- Scala 2.12.4: Cannot access protected static Java method from another package anymore
- scala collections : map a list and carry some state?
- How are Scala closures transformed to Java objects?
- Monix TaskLocal does not seem to reflect values from bind() call when referenced by Task by from different lexical scope
- Setting format of setters & getters in Scala case classes
- How to run Scala REPL commands in ammonite REPL/SHELL?
- type mismatch; found : cats.Show[shapeless.CNil] required: cats.Show[A] after adding scalac option -Ypartial-unification in sbt
- Using Netty 4 with Akka (Scala)
- groupBy method throws an error in Slick
- Completing Source[ByteString, _] in Akka-Http
- Get rows with common column values in output
- Apache Spark - Reduce Step Output (K, (V1,V2, V3, ...)
- Why does scala.util.Success.flatMap (Try.flatMap) use try-catch again after applying its argument?
- Scala: Use case for Array.toArray?
- Get the date ranges excluding the stoppage timings
- Append file name hash to each line of a Spark RDD
- how to parse postman-collection?
- Play Framework 20 - Creating a form in the view