Stefan Endrullis
Stefan Endrullis

Reputation: 4208

How to suppress "match is not exhaustive!" warning in Scala

How can I suppress the "match is not exhaustive!" warning in the following Scala code?

val l = "1" :: "2" :: Nil
l.sliding(2).foreach{case List(a,b) => }

The only solution that I found so far is to surround the pattern matching with an additional match statement:

l.sliding(2).foreach{x => (x: @unchecked) match {case List(a,b) => }}

However this makes the code unnecessarily complex and pretty unreadable. So there must be a shorter and more readable alternative. Does someone know one?

Edit

I forgot to mention that my list l has at least 2 elements in my program. That's why I can safely suppress the warning.

Upvotes: 24

Views: 8641

Answers (4)

comonad
comonad

Reputation: 5261

implicit class RichAny[A](private val a: A) extends AnyVal {
  @inline
  def match_ignoring_nonexhaustive[B](f: PartialFunction[A,B]): B = f(a)
}

With this you could do the following, which actually only interprets the case match as PartialFunction:

l.sliding(2).foreach{ _ match_ignoring_nonexhaustive {case List(a,b) => }}

Upvotes: 1

Steve
Steve

Reputation: 3068

Here are several options:

  1. You can match against Seq instead of List, since Seq doesn't have the exhaustiveness checking (this will fail, like your original, on one element lists):

    l.sliding(2).foreach{case Seq(a, b) => ... }
    
  2. You can use a for comprehension, which will silently discard anything that doesn't match (so it will do nothing on one element lists):

    for (List(a, b) <- l.sliding(2)) { ... }
    
  3. You can use collect, which will also silently discard anything that doesn't match (and where you'll get an iterator back, which you'll have to iterate through if you need to):

    l.sliding(2).collect{case List(a,b) => }.toList
    

Upvotes: 23

Lomig M&#233;gard
Lomig M&#233;gard

Reputation: 1838

Since your sliding(2) can possibly return one last list with only one element in it, you should also test it:

l sliding(2) foreach {
  case a::b::Nil => println("two elements: " + a + b)
  case l         => println("one last element" + l.head)
}

Upvotes: 3

Rex Kerr
Rex Kerr

Reputation: 167921

Making it complete with ; case _ => ??? is pretty short. ??? just throws an exception. You can define your own if you're using 2.9 or before (it's new in 2.10).

It really is pretty short compared to what you need for a match annotation:

(: @unchecked)
; case _ => ???
              ^  One more character!

It doesn't throw a MatchError, but does that really matter?

Upvotes: 21

Related Questions