Scala Predef unimport [duplicate]

十年热恋 提交于 2019-12-04 00:40:32

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7634015/scala-predef-unimport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!