1

I've a piece of code for which I get a "match may not be exhaustive" warning from Scala 2.13.4, and I'd like to suppress that warning with the @unchecked annotation. Unfortunately, all my attempts of inserting @unchecked merely resulted in syntax errors.

Here's a mercilessly simplified version of the original code:

def foo(xs: Seq[Int], n: Int)(f: (Seq[Int], Int) => Int): Int = f(xs, n)
    
foo(Seq(1,2), 0) { case (Seq(a,b), c) => a + b + c }

Question: Where do I syntactically put @unchecked at call site in order to suppress the warning?

P.S. I'd like to suppress the warning, not start a discussion of whether or not that is evil ;-)

Malte Schwerhoff
  • 12,684
  • 4
  • 41
  • 71
  • 1
    I am not sure that I've got it right, but I can't reproduce it: https://scastie.scala-lang.org/toshetah/jujYc1IDTx20lIRZSGjPBA/3 – Tomer Shetah Jan 08 '21 at 23:23
  • @TomerShetah Interesting - I'll check what the project's compiler options are, maybe there's something special there. Either way, I'd still like to find out where to put `@unchecked`. – Malte Schwerhoff Jan 10 '21 at 15:25
  • @TomerShetah Not need to reproduce the warning: if you managed to squeeze in `@unchecked` without causing compiler errors, you'd have helped me already :-) – Malte Schwerhoff Jan 11 '21 at 10:52
  • 1
    I hope I have enough unchecked in my code to help you :) – Tomer Shetah Jan 11 '21 at 17:50

3 Answers3

1

Not sure if I've understood this correctly but have you tried something like this:

def pie(x: Option[String]) =
(x: @unchecked) match {
  case Some(v) => v
}

The warning is removed (for me) with this syntax

Paul Brown
  • 161
  • 6
1

I am not really sure what is that good for, but I think this is the maximum unchecked you can do in the code in the question:

def foo(xs: Seq[Int @unchecked] @unchecked, n: Int @unchecked)(f: (Seq[Int @unchecked] @unchecked, Int @unchecked) => Int @unchecked): Int @unchecked = {
  f(xs, n)
}

println(foo(Seq(1,2), 0) {
  case (Seq(a: Int @unchecked,b: Int @unchecked), c: Int @unchecked) => a + b + c
})

unchecked in Scaladoc. Code run at Scastie.

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
  • That are indeed a lot of `@unchecked` :-) These suppress checks for every use of `foo` (since they are placed at definition site), but what I'd like is to suppress is the check at call-site, where there is an implicit pattern matching — at call site — in the form of the partial function that is passed as the second argument to `foo`. I've clarified my question, and in case I find a solution myself, I'll get back to you. – Malte Schwerhoff Jan 12 '21 at 16:28
  • 1
    @MalteSchwerhoff, does I've updated my answer. Does that help you? – Tomer Shetah Jan 12 '21 at 23:30
  • I see you like a challenge :-) Unfortunately, it doesn't help, probably because the annotations affect the elements of the sequence, rather than the sequence itself. I.e. they don't suppress complaints about not matching against arbitrary-sized sequences. It is obvious that debugging blindly (since you don't get the compiler warning) is not ideal. I unfortunately didn't yet manage to distil a sufficiently small example that still causes the warnings :-/ – Malte Schwerhoff Jan 18 '21 at 17:29
1

I didn't get warnings after enabling "unchecked" in compiler options in IntelliJ. It's not scalastyle, is it? I appreciate your example is contrived. Does either of these approaches work for you?

Option 1: Defining your curried function as a PartialFunction

def foo(xs: Seq[Int], n: Int) (f: PartialFunction[(Seq[Int], Int), Int]): Int = 
    if(f.isDefinedAt(xs, n))
        f(xs, n)
    else 0

Option 2: Converting your case statement to a match expression?

foo(Seq(1,2), 0) {
  (_:(Seq[Int], Int) @unchecked) match
  {
    case (Seq(a, b), c) => a + b + c
  }
}
sparker
  • 1,245
  • 11
  • 17