Search code examples
scalacompiler-warningsstring-interpolation

Disable false warning "possible missing interpolator"


I have some nested case classes, and for a specific serialization mechanism I need to provide a productPrefix that contains $ characters. Like so

object Foo {
  case class Bar() {
    override def productPrefix = "Foo$Bar"
  }
}

Now I get a compiler warning from this:

Warning:(53, 42) possible missing interpolator: detected interpolated identifier `$Bar`
    override def productPrefix: String = "Foo$Bar"

Is there a way to disable the warning, if possible only for this instance? Scala version is 2.11.8.


Edit: I just thought I was clever: "Foo\u0024Bar". But the compiler even warns about this. Another solution is classOf[Bar].getName, given the constraint that there are no further outer objects.


Solution

  • If there's a way to compose the string from its parts you could do something like this.

    override def productPrefix = "Foo$" + "Bar"
    

    You could also quiet the "missing interpolator" warning by actually invoking the interpolator, but escaping the $.

    override def productPrefix = f"Foo$$Bar"