score:247
What you're looking for is a stable identifier. In Scala, these must either start with an uppercase letter, or be surrounded by backticks.
Both of these would be solutions to your problem:
def mMatch(s: String) = {
val target: String = "a"
s match {
case `target` => println("It was" + target)
case _ => println("It was something else")
}
}
def mMatch2(s: String) = {
val Target: String = "a"
s match {
case Target => println("It was" + Target)
case _ => println("It was something else")
}
}
To avoid accidentally referring to variables that already existed in the enclosing scope, I think it makes sense that the default behaviour is for lowercase patterns to be variables and not stable identifiers. Only when you see something beginning with upper case, or in back ticks, do you need to be aware that it comes from the surrounding scope.
score:2
You can also assign it to a temporary variable inside the case and then compare it, that will also work
def mMatch(s: String) = {
val target: String = "a"
s match {
case value if (value ==target) => println("It was" + target) //else {} optional
case _ => println("It was something else")
}
}
Source: stackoverflow.com
Related Query
- Why does pattern matching in Scala not work with variables?
- Why does Scala not infer the type parameters when pattern matching with @
- Why does Regex pattern matching not work sometimes in Scala
- Why does this snippet with pattern matching and higher kinded types no longer compile in Scala 2.12?
- Why does Scala implicit conversion work here with two args but not with one?
- Why does Scala regexp work differently in pattern matching
- IntelliJ IDEA Hotkey for comment does not work with Scala
- 7.toBinayString does not work in scala REPL, but with val k=7 it works
- Why scala's pattern maching does not work in for loops for type matching?
- Does it make any sense to use pattern matching in Scala with really simple cases?
- Why does overloading polymorphic methods with different upper bounds not compile in Scala
- Why scala does not unify this type lambda with underlying type?
- Command `timeout` does not work with Scala - why?
- Why can I use :: operator with Seq in pattern matching but not elsewhere
- why yield can not work with while loop in scala
- Why the scala :_* to expand a Seq into variable-length argument list does not work in this case?
- Why does map/filter ... not work with an Array of Nothing?
- Why does the same scala code work OK in command line while not in Intellij?
- Why does Scala require pattern variables to be linear?
- Scala pattern matching with disjunctions not working
- Simple file read with Scala does not work
- Why does Scala 2.10 give 'match may not be exhaustive' warning when matching on singleton types?
- why currying does not work with a function literal?
- Why does $ not work with values of type String (and only with the string literals directly)?
- Why does this Scala placeholder not work
- Why does activator fail with "Could not retrieve Scala 2.11.5: missing scala.Option, scala.tools.nsc.Global" at startup?
- pattern matching in scala works inside a def, otherwise does not
- Why does not sequence work with List of Validations
- Why does Scala reify not work as according to the docs?
- Why does Jackson ObjectReader `with` function not work with Scala?
More Query from same tag
- Find which interval a number belongs to
- meaning of top level private class in scala
- How to parse dynamic json reponse and get specific value and pass it as an input to next request
- Dynamically choose implementation of logger in Scala
- Numbers generation bad value
- Spark Window function using more than one column
- Using Scala Slick with database enums
- Idiomatic way to handle multiple concurrent streams in Scala
- How to delete useless registers generated by Chisel verilog backend?
- Build error for Scala Slick 2.0.0
- Using lazy evaluation functions in varargs
- Multiple sources found for csv : readStream
- Is there a way to have a Java Swing application as a child of a native HWND window?
- How to check gaps in a String sequence derived from file names
- Method named "*" cause compile error
- how to upload the excel file in akka http micro services
- Disruptor – Last handler not invoked
- "illegal start of simple expression" error in scalajs when use :=
- How to override libraryDependencies in a sbt plugin?
- String to Key-Value pairs in spark dataframe (No Pyspark)
- Weird situation with Scala Play - type inference
- scala foreach with one augment, cannot take any operation?
- Supporting quotations and string substitutions together in Scala
- Multiple scala libraies causing error in intellij?
- Returning <Case Class>.type with Case Class
- How to load a NodeSeq from a string?
- how to retrieve A scala mutable Set from mongo
- Left JOIN with AND clause in ON with scalikejdbc
- Spark Scala - DataFrames & csv - partial extraction of values
- Akka cluster detecting Quarantined state