添加默认包导入

发布于 2024-10-17 23:24:53 字数 251 浏览 2 评论 0原文

在 Java、Scala 或一般任何 JVM 语言中,都有一组默认导入的包。例如,Java 会自动导入 java.lang,您无需在 Java 代码文件中执行此操作。

现在我不知道哪个组件准确地处理这个问题(编译器?JVM?),但是有什么方法可以默认导入其他包甚至类吗?

假设您有一个包,定义了您在整个项目中使用的一组实用函数(例如 Scala 中的 scala.math ),如果您能够跳过它的导入,那就太好了每门数学相关课程。

In Java, Scala, or generally any JVM language, there is a set of packages that is imported by default. Java, for instance, automatically imports java.lang, you don't need to do it in your Java code file.

Now I don't know which component takes care of this exactly (the compiler? the JVM?), but is there any way to have additional packages or even classes imported by default?

Say you have a package defining a set of utility functions you use throughout your project (an example could be scala.math in Scala), it would be great if you were able to skip the import for it in every math related class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情域 2024-10-24 23:24:53

从 2.8 开始,Scala 具有包对象,其内容会自动导入到包的每个文件中。在包的顶级目录(例如,x/y/z/mypackage)中创建一个名为 package.scala 的文件,并将其放入其中

package x.y.z

package object mypackage {
  ...
}

然后自动导入包对象中的任何定义(即,不需要 import 语句)到包 xyzmypackage 中的任何文件中。

您可以查看 Scala 源代码——执行 find dir -name package.scala——有很多源代码,最顶层定义了scala 包本身,它会自动导入到每个 scala 源文件中。

Scala, as of 2.8, has package objects, the contents of which are automatically imported into every file of the package. Create a file called package.scala in the top level directory of your package (e.g., x/y/z/mypackage), and put into it

package x.y.z

package object mypackage {
  ...
}

Then any definition within the package object is automatically imported (i.e., no import statement is needed) into any file in package x.y.z.mypackage

You can take a look at the Scala sources -- do a find dir -name package.scala -- there are a bunch of them, the topmost defining the implicit imports for the scala package itself, which are automatically imported into every scala source file.

在梵高的星空下 2024-10-24 23:24:53

Jim Balter他的答案(投票吧)Scala 2.8 包对象

任何类型的定义都可以放在类中,也可以放在包的顶层。如果您希望某些辅助方法处于整个包的范围内,请继续将其放在包的顶层。

为此,请将定义放入包对象中。每个包允许有一个包对象

Scala 实际上有自己的 scala 包对象
请参阅“放置包对象的位置”。

所以包对象可以解决给定包的显式导入问题,但不能解决任何包的显式导入问题。
当前限制包括

  • 首先,您不能在包对象中定义或继承重载方法。
  • 其次,您不能定义或继承包对象中的成员,该成员也是同一包中顶级类或对象的名称。

我们预计未来的某些 Scala 版本将取消这些限制。


原始答案:

在Scala方面,像“Scala 默认导入 scala.collection.JavaConversions._ 吗?" 显示您不能仅以 Scala 用户身份添加默认导入。
它必须得到语言的支持。

顺便说一句,结论是:

从 Scala 专家的角度来看:让我们保持简单并将默认导入的隐式限制为绝对最小值。
海科·西伯格


拉斐尔< /a> 但确实发表了评论:

我想你可以编写自己的编译器插件来添加一些包?

果然,这篇 Scala 文章描述了如何扩展 Scala 编译器
但支持的主要补充是:

  • 您可以向编译器添加一个阶段,从而添加在类型检查完成后应用的额外检查或额外的树重写。
  • 您可以告诉编译器有关要应用于类型的注释的类型检查信息。

因此,即使进行树重写(包括额外的导入),我也不确定所说的重写是否会及时完成,以便正确分析类型和函数(由您要求的隐式导入引用)。
但也许 Scala 编译器角对此有更多想法。

Jim Balter mentions in his answer (go upvote it) the Scala 2.8 package object.

Any kind of definition that you can put inside a class, you can also put at the top level of a package. If you have some helper method you'd like to be in scope for an entire package, go ahead and put it right at the top level of the package.

To do so, put the definitions in a package object. Each package is allowed to have one package object

Scala actually has its own scala package object.
See "Where to put package objects".

So package object can solve the problem of explicit import for a given package, but not for any package.
Its current limitations include:

  • First, you cannot define or inherit overloaded methods in package objects.
  • Second, you cannot define or inherit a member in a package object which is also the name of a top-level class or object in same package.

We expect that some future Scala release will drop these restrictions.


Original answer:

On the Scala side, a question like "should Scala import scala.collection.JavaConversions._ by default?" shows that you cannot just add default imports as a Scala user.
It has to be supported by the language.

By the way, the conclusion was:

From a Scala expert perspective: Let's keep things simple and limit the implicits that are imported by default to the absolute minimum.
Heiko Seeberger


Raphael does comment though:

I guess you could write your own compiler plugin to add some packages?

And sure enough, this Scala article describe how to extend the Scala compiler.
But the main additions supported are:

  • You can add a phase to the compiler, thus adding extra checks or extra tree rewrites that apply after type checking has finished.
  • You can tell the compiler type-checking information about an annotation that is intended to be applied to types.

So even with a tree rewrite (including additional imports), I am not sure said rewrite would be done in time for the types and functions (referenced by the implicit imports you are asking for) can be correctly analyzed.
But maybe the Scala Compiler Corner has more ideas on that.

记忆消瘦 2024-10-24 23:24:53

java.lang 中所有类的默认导入在 Java 语言规范 (7.5.5)

所有其他类都必须使用 import 语句导入。

The default import of all classes from java.lang is specified in the Java language Specification (7.5.5)

All other classes have to be import with the import statement.

被翻牌 2024-10-24 23:24:53

在 Java 中没有办法做到这一点。

In Java there is not a way to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文