执行类似 Python 的“导入”操作在斯卡拉

发布于 2024-11-04 21:08:47 字数 363 浏览 1 评论 0原文

是否可以使用 Scala 的 import 而不在对象中指定 main 函数,并且不在源文件中使用要导入的代码的 package 关键字?

一些解释:在Python中,我可以在某个文件“Lib.py”中定义一些函数,

from Lib import *

在同一目录中写入其他文件“Run.py”,在Run中使用Lib中的函数,然后使用命令运行Run python Run.py。此工作流程非常适合我可能在一小时内编写的小型脚本。

在 Scala 中,如果我想包含另一个文件中的函数,我需要开始将东西包装在多余的对象中。我宁愿不这样做。

Is it possible to use Scala's import without specifying a main function in an object, and without using the package keyword in the source file with the code you wish to import?

Some explanation: In Python, I can define some functions in some file "Lib.py", write

from Lib import *

in some other file "Run.py" in the same directory, use the functions from Lib in Run, and then run Run with the command python Run.py. This workflow is ideal for small scripts that I might write in an hour.

In Scala, it appears that if I want to include functions from another file, I need to start wrapping things in superfluous objects. I would rather not do this.

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

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

发布评论

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

评论(4

烟柳画桥 2024-11-11 21:08:47

用 Scala 编写 Python 不太可能产生令人满意的结果。对象并不是“多余的”——而是你的程序不是以面向对象的方式编写的。

首先,方法必须位于对象内部。您可以将它们放置在包对象内,然后它们对于同名包内的任何其他内容都是可见的。

其次,如果只考虑对象和类,那么所有类文件存在于类路径中或者其 scala 文件编译在一起的无包对象和类将彼此可见。

Writing Python in Scala is unlikely to yield satisfactory results. Objects are not "superfluous" -- it's your program that is not written in an object oriented way.

First, methods must be inside objects. You can place them inside a package object, and they'll then be visible to anything else that is inside the package of the same name.

Second, if one considers solely objects and classes, then all package-less objects and classes whose class files are present in the classpath, or whose scala files are compiled together, will be visible to each other.

初懵 2024-11-11 21:08:47

这是我能得到的最小的:

[$]> cat foo.scala
object Foo {
   def foo(): Boolean = {
      return true
   }
}

// vim: set ts=4 sw=4 et:
[$]> cat bar.scala
object Bar extends App {
    import Foo._

    println(foo)
}

// vim: set ts=4 sw=4 et:
[$]> fsc foo.scala bar.scala
[$]> export CLASSPATH=.:$CLASSPATH # Or else it can't find Bar.
[$]> scala Bar
true

This is as minimal as I could get it:

[$]> cat foo.scala
object Foo {
   def foo(): Boolean = {
      return true
   }
}

// vim: set ts=4 sw=4 et:
[$]> cat bar.scala
object Bar extends App {
    import Foo._

    println(foo)
}

// vim: set ts=4 sw=4 et:
[$]> fsc foo.scala bar.scala
[$]> export CLASSPATH=.:$CLASSPATH # Or else it can't find Bar.
[$]> scala Bar
true
情徒 2024-11-11 21:08:47

当您只编写简单的脚本时,请使用 Scala 的 REPL。在那里,您可以定义函数并调用它们,而无需任何封闭对象或包,也无需 main 方法。

When you just write simple scripts, use Scala's REPL. There, you can define functions and call them without having any enclosing object or package, and without a main method.

神经暖 2024-11-11 21:08:47

对象/类不必位于包中,但强烈建议这样做。也就是说,您还可以像包一样对待单例对象,即作为独立函数的命名空间,并像包一样导入它们的内容。

如果您将应用程序定义为扩展 App 的对象,则不必定义 main 方法。只需在对象主体中编写代码,App 特征(扩展了特殊的 DelayedInit 特征)将提供一个执行您的代码的 main 方法。

如果只想编写脚本,您可以完全放弃对象,只编写没有任何容器的代码,然后以非交互模式将源文件传递给解释器(REPL)。

Objects/classes don't have to be in packages, though it's highly recommended. That said, you can also treat singleton objects like packages, i.e., as namespaces for standalone functions, and import their contents as if they were packages.

If you define your application as an object that extends App, then you don't have to define a main method. Just write your code in the body of the object, and the App trait (which extends thespecial DelayedInit trait) will provide a main method that will execute your code.

If just want to write a script, you can forgo the object altogether and just write code without any container, then pass your source file to the interpreter (REPL) in non-interactive mode.

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