本文介绍了Scala Predef unmport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,Predef中的内容会自动导入到scala程序中。但是如何禁用 - 从Predef中取消导入某些或所有导入的函数?作为一个例子,如果我不喜欢String上的'+'运算符如何禁用此功能?

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?

推荐答案

如上所述链接的答案,方法 String#+(其他:任何)被添加到具有编译魔术的String类,而不是隐式转换。因此,它与 Predef ._ 的自动导入无关。

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._.

这同样适用于 Int#+(x:String),以及其他值类型的相应方法。

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

然而,还有另一个通过 Predef 中的隐式转换添加 的字符串连接方法。 x +2被视为 Predef.any2stringAdd(x)。+(2)。通过在文件的第一行显式导入 Predef ,您可以将不需要的成员重命名为 _ ,并禁用它们。

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
}

我认为这不适用于Scala脚本或REPL。还有一个不受支持的选项 -Yno-predef ,用于全局自动导入。

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.

相关:

这篇关于Scala Predef unmport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 13:53