admin 管理员组文章数量: 1086019
I have a MyScript.scala
file:
//> using scala "3"
object MyUtils {
def sayHello() = println("Hello")
}
@main
def main(): Unit =
MyUtils.sayHello();
I successfully run it like this:
> scala MyScript.scala
Compiling project (Scala 3.6.4, JVM (17))
Compiled project (Scala 3.6.4, JVM (17))
Hello
Question: What do I need to do to move my MyUtils
object into another source file (e.g., MyUtils.scala
or MyUtils.sc
) and still call the sayHello
method from within MyScript.scala
?
I have a MyScript.scala
file:
//> using scala "3"
object MyUtils {
def sayHello() = println("Hello")
}
@main
def main(): Unit =
MyUtils.sayHello();
I successfully run it like this:
> scala MyScript.scala
Compiling project (Scala 3.6.4, JVM (17))
Compiled project (Scala 3.6.4, JVM (17))
Hello
Question: What do I need to do to move my MyUtils
object into another source file (e.g., MyUtils.scala
or MyUtils.sc
) and still call the sayHello
method from within MyScript.scala
?
1 Answer
Reset to default 3With the Scala CLI, you can use the using file
directive.
For instance, in the main file:
//> using scala "3"
//> using file Utils.scala
@main
def main(): Unit = {
MyUtils.sayHello()
}
And the imported file Utils.scala
:
object MyUtils {
def sayHello() = println("Hello")
}
Reference : https://scala-cli.virtuslab./docs/guides/scripting/scripts#define-source-files-in-using-directives
本文标签: Importincludeusing objects from another source file into a Scala script fileStack Overflow
版权声明:本文标题:Importincludeusing objects from another source file into a Scala script file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744016202a2518964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论