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 best I can come up with is this:

```
def matches[A](a:A)(f:PartialFunction[A, Unit]) = f.isDefinedAt(a)

if (matches(a){case ... =>}) {
    //do stuff
}

```

This won't win you any style points though.
[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)).