为什么主函数没有在 REPL 中运行? [英] Why is the main function not running in the REPL?

查看:26
本文介绍了为什么主函数没有在 REPL 中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的程序.我希望 main 在解释模式下运行.但是另一个物体的存在导致它什么也不做.如果 QSort 不存在,程序就会执行.

为什么当我在 REPL 中运行它时没有调用 main?

object MainObject{def main(args: Array[String])={val unsorted = List(8,3,1,0,4,6,4,6,5)打印(你好"+未排序的toString)//val sorted = QSort(未排序)//排序foreach println}}//这个不能存在对象 QSort{def apply(array: List[Int]):List[Int]={大批}}

<小时>

抱歉造成混淆,我以 scala filename.scala 运行脚本.

解决方案

发生了什么

如果 scala 的参数是一个现有的 .scala 文件,它将在内存中编译并运行.当只有一个顶级对象时,将搜索 main 方法,如果找到,则执行.如果不是这种情况,顶级语句将包含在合成的 main 方法中,该方法将改为执行.

这就是删除顶级 QSort 对象允许您的主要方法运行的原因.

如果您要将其扩展为完整程序,我建议编译并运行(使用诸如 sbt 之类的构建工具)已编译的 .class 文件:

scalac main.scala &&Scala 主对象

如果您正在编写单个文件脚本,只需删除 main 方法(及其对象)并编写您要在外部作用域中执行的语句,例如:

//qsort.scala对象 QSort{def apply(array: List[Int]):List[Int]={大批}}val unsorted = List(8,3,1,0,4,6,4,6,5)打印(你好"+未排序的toString)val 排序 = QSort(未排序)已排序的 foreach println

并运行:scala qsort.scala

一点背景

scala 命令用于执行 scala脚本"(单个文件程序)和复杂的类 java 程序(在类路径中包含一个主对象和一堆类).

来自man scala:

 scala 实用程序使用 Java 运行时环境运行 Scala 代码.要运行的 Scala 代码通过以下三种方式之一指定:1. 没有指定参数,Scala shell 启动并读取com-交互式命令.2. 指定 -howtorun:object 后,a 的完全限定名称可以指定顶级 Scala 对象.该对象应预先以前是使用 scalac(1) 编译的.3. 指定 -howtorun:script 后,包含 Scala 代码的文件可以指定.

如果没有明确指定,howtorun 模式是从传递给脚本的参数中推测出来的.

当给定一个对象的完全限定名称时,scala 会猜测 -howtorun:object 并期望在路径上有一个具有该名称的编译对象.

否则,如果 scala 的参数是现有的 .scala 文件,则猜测 -howtorun:script 并按上述方式选择入口点.

This is a simple program. I expected main to run in interpreted mode. But the presence of another object caused it to do nothing. If the QSort were not present, the program would have executed.

Why is main not called when I run this in the REPL?

object MainObject{
  def main(args: Array[String])={
    val unsorted = List(8,3,1,0,4,6,4,6,5)
    print("hello" + unsorted toString)
    //val sorted = QSort(unsorted)
    //sorted foreach println
  }
}

//this must not be present

object QSort{
  def apply(array: List[Int]):List[Int]={
    array
  }
}


EDIT: Sorry for causing confusion, I am running the script as scala filename.scala.

解决方案

What's happening

If the parameter to scala is an existing .scala file, it will be compiled in-memory and run. When there is a single top level object a main method will be searched and, if found, executed. If that's not the case the top level statements are wrapped in a synthetic main method which will get executed instead.

This is why removing the top-level QSort objects allows your main method to run.

If you're going to expand this to a full program, I advise to compile and run (use a build tool like sbt) the compiled .class files:

scalac main.scala && scala MainObject

If you're writing a single file script, just drop the main method (and its object) and write the statements you want executed in the outer scope, like:

// qsort.scala
object QSort{
  def apply(array: List[Int]):List[Int]={
    array
  }
}

val unsorted = List(8,3,1,0,4,6,4,6,5)
print("hello" + unsorted toString)
val sorted = QSort(unsorted)
sorted foreach println

and run with: scala qsort.scala

A little context

The scala command is meant for executing both scala "scripts" (single file programs) and complex java-like programs (with a main object and a bunch of classes in the classpath).

From man scala:

   The  scala  utility  runs  Scala code using a Java runtime environment.
   The Scala code to run is specified in one of three ways:

      1.  With no arguments specified, a Scala shell starts and reads com-
          mands interactively.

      2.  With  -howtorun:object  specified, the fully qualified name of a
          top-level Scala object may be specified.  The object should pre-
          viously have been compiled using scalac(1).

      3.  With  -howtorun:script  specified,  a file containing Scala code
          may be specified.

If not explicitly specified, the howtorun mode is guessed from the arguments passed to the script.

When given a fully qualified name of an object, scala will guess -howtorun:object and expect a compiled object with that name on the path.

Otherwise, if the parameter to scala is an existing .scala file, -howtorun:script is guessed and the entry point is selected as described above.

这篇关于为什么主函数没有在 REPL 中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆