Question: I found myself writing something like this quite often:

```
a match {     
  case `b` => // do stuff
  case _ => // do nothing
}

```

Is there a shorter way to check if some value matches a pattern? I mean, in this case I could just write `if (a == b) // do stuff`, but what if the pattern is more complex? Like when matching against a list or any pattern of arbitrary complexity. I'd like to be able to write something like this:

```
if (a matches b) // do stuff

```

I'm relatively new to Scala, so please pardon, if I'm missing something big :)

Answer: 
The `match` operator in Scala is most powerful when used in functional style. This means, rather than "doing something" in the `case` statements, you would return a useful value. Here is an example for an imperative style:

```
var value:Int = 23
val command:String = ... // we get this from somewhere
command match {
  case "duplicate" => value = value * 2
  case "negate" => value = -value
  case "increment" => value = value + 1
  // etc.
  case _ => // do nothing
}
println("Result: " + value)

```

It is very understandable that the "do nothing" above hurts a little, because it seems superflous. However, this is due to the fact that the above is written in imperative style. While constructs like these may sometimes be necessary, in many cases you can refactor your code to functional style:

```
val value:Int = 23
val command:String = ... // we get this from somewhere
val result:Int = command match {
   case "duplicate" => value * 2
   case "negate" => -value
   case "increment" => value + 1
   // etc.
   case _ => value
}
println("Result: " + result)

```

In this case, you use the whole `match` statement as a value that you can, for example, assign to a variable. And it is also much more obvious that the `match` statement must return a value in any case; if the last case would be missing, the compiler could not just make something up.

It is a question of taste, but some developers consider this style to be more transparent and easier to handle in more real-world examples. I would bet that the inventors of the Scala programming language had a more functional use in mind for `match`, and indeed the `if` statement makes more sense if you only need to decide whether or not a certain action needs to be taken. (On the other hand, you can also use `if` in the functional way, because it also has a return value...)
[Kim's answer](https://stackoverflow.com/questions/4437061/scala-short-form-of-pattern-matching-that-returns-boolean/4437451#4437451) can be “improved” to better match your requirement:

```
class AnyWrapper[A](wrapped: A) {
  def matches(f: PartialFunction[A, Unit]) = f.isDefinedAt(wrapped)
}
implicit def any2wrapper[A](wrapped: A) = new AnyWrapper(wrapped)

```

then:

```
val a = "a" :: Nil
if (a matches { case "a" :: Nil => }) {
  println("match")
}

```

I wouldn't do it, however. The `=> }) {` sequence is really ugly here, and the whole code looks much less clear than a normal match. Plus, you get the compile-time overhead of looking up the implicit conversion, and the run-time overhead of wrapping the match in a `PartialFunction` (not counting the conflicts you could get with other, already defined `matches` methods, like the one in `String`).

To look a little bit better (and be less verbose), you could add this def to `AnyWrapper`:

```
def ifMatch(f: PartialFunction[A, Unit]): Unit = if (f.isDefinedAt(wrapped)) f(wrapped)

```

and use it like this:

```
a ifMatch { case "a" :: Nil => println("match") }

```

which saves you your `case _ =>` line, but requires double braces if you want a block instead of a single statement... Not so nice.

Note that this construct is not really in the spirit of functional programming, as it can only be used to execute something that has side effects. We can't easily use it to return a value (therefore the `Unit` return value), as the function is partial — we'd need a default value, or we could return an `Option` instance. But here again, we would probably unwrap it with a match, so we'd gain nothing.

Frankly, you're better off getting used to seeing and using those `match` frequently, and moving away from this kind of imperative-style constructs (following [Madoc's nice explanation](https://stackoverflow.com/questions/4437061/scala-short-form-of-pattern-matching-that-returns-boolean/4437651#4437651)).