开发者

Scala Predef unimport [duplicate]

This question already has answers here: Closed 11 years ago.

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜