How to run a Scala script within IntelliJ IDEA?

前端 未结 4 1977
野的像风
野的像风 2021-02-05 02:05

Here is a trivial scala script:

object test {
  def hi() { print(\"hi there from here\") }
}


test.hi()

From the command line it does the expe

4条回答
  •  我在风中等你
    2021-02-05 02:54

    Your code works in command line because it is "script", when you want to make it runnable in project there are ways to do it:

    1. By creating object which extends App

      object test {
          def hi() { print("hi there from here") }
      }
      
      object runnable extends App {
          test.hi()
      }
      
    2. or java-like solution i.e. creating main method

      object test {
          def hi() { print("hi there from here") }
      }
      
      object runnable {
          def main(args: Array[String]) {
              test.hi()
          }
      }
      
    3. when need to execute as script - I do it like that: select code to execute by mouse, then choose from context menu "Send Selection To Scala Console"

提交回复
热议问题