Question

Possible Duplicate:
How to unimport String “+” operator in Scala?

So things from Predef get automatically imported into scala programs. But how can I disable- unimport certain or all imported functions from Predef? As an example if I don't like the '+' operator on String how to disable this functionality?

Was it helpful?

Solution

As mentioned in the linked answer, the method String#+(other: Any) is added to the String class with compiler magic, rather than with an implicit conversion. As such, it isn't related to the automatic import of Predef._.

The same applies to Int#+(x: String), and corresponding method on the other value types.

However, there is another String concatenation method that is added by an implicit conversion in Predef. x + "2" is treated as Predef.any2stringAdd(x).+("2"). By explicitly importing Predef on the first line of your file, you can rename unwanted members to _, disabling them.

import Predef.{any2stringadd => _, _}

object Test {    
  object A
  A + "20" // error: value + is not a member of object Test.A
}

I don't think that this works in Scala Scripts or in the REPL. There is also an unsupported option, -Yno-predef, to turn of the automatic import globally.

Related: SI-1931

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top