Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Scala warning "The outer reference in this type test cannot be checked at run time"

I understand what the warning "The outer reference in this type test cannot be checked at run time" means (final case class is missing its "outer" pointer if it is not used in the class itself). Still, I find this warning extremely annoying, as it is displayed even when I am not even matching against the type, like in the code below:

object X {
  class B {
    final case class A(a: Int)
  }

  def main(arg: Array[String]) = {

  }
}

How can I disable this warning, other than making the case class not final? Using @unchecked before the case class definition does not help.

like image 894
Suma Avatar asked Feb 01 '20 13:02

Suma


1 Answers

Perhaps as a workaround we could make case class in effect final by making the constructor private

class B {
  case class X private (s: String)
}

which prevents inheritance

class C extends X("") // Error

whilst still allowing construction due to public apply in the companion

(new B).X("")         // OK

The bug does not seem to be present in dotty so once Scala 3 is released it should be possible to revert to final case class (with perhaps regex replacement).

like image 80
Mario Galic Avatar answered Dec 06 '22 08:12

Mario Galic