最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Importincludeusing objects from another source file into a Scala script file - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited Mar 28 at 20:11 Wallace asked Mar 28 at 19:52 WallaceWallace 17.6k9 gold badges60 silver badges83 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

With 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

发布评论

评论列表(0)

  1. 暂无评论