Scala 给了我“非法的定义开始”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2632247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala giving me "illegal start of definition"
提问by Malvolio
I'm trying to get started with Scala and cannot get out of the starting gate.
我正在尝试开始使用 Scala,但无法走出起点。
A file consisting of the line
由行组成的文件
package x
gives me
给我
error: illegal start of definition
错误:定义的非法开始
Regardless of what x is and regardless of where I put the file (I had a theory that I had to place the file in a directory hierarchy to match the package definition, but no). I get the same error with the example code from the web site and with the REPL.
不管 x 是什么,也不管我把文件放在哪里(我有一个理论,我必须将文件放在目录层次结构中以匹配包定义,但没有)。我在使用来自网站的示例代码和 REPL 时遇到了同样的错误。
回答by Ben James
It looks like you're trying to declare the packagemembership in a Scala script (run using the scalacommand) or in the REPL.
看起来您正在尝试package在 Scala 脚本(使用scala命令运行)或 REPL 中声明成员资格。
Only files defining just classes and objects which are compiled with scalacmay be defined as belonging to a package.
只有定义类和对象的文件scalac才能被定义为属于一个包。
When you run code in a script or a REPL session, behind the scenes it is actually compiled inside a method of an object, in which scope a package declaration wouldn't be legal.
当您在脚本或 REPL 会话中运行代码时,在幕后它实际上是在对象的方法内编译的,在该范围内,包声明是不合法的。
回答by qtwo
Since Scala 2.11.0-M7 you can use :paste -raw(fix for issue SI-5299). This option allows defining packages in the REPL:
从 Scala 2.11.0-M7 开始,您可以使用:paste -raw(修复问题SI-5299)。此选项允许在 REPL 中定义包:
scala> :paste -raw
// Entering paste mode (ctrl-D to finish)
package Foo
class Bar
// Exiting paste mode, now interpreting.
scala> import Foo._
import Foo._
scala> new Bar
res1: Foo.Bar = Foo.Bar@3ee2cf81
回答by dehasi
I had the same problem. I've resolved it by importing import packageName._instead of declaring a worksheet in the package.
我有同样的问题。我已经通过导入import packageName._而不是在包中声明工作表来解决它。
回答by Milan Bojovic
I had the same issue when I was executing scala program eg. "Game.scala"from terminal.
我在执行 scala 程序时遇到了同样的问题,例如。来自终端的“Game.scala”。
Compiling part was ok, error was shown when running the code, see below
编译部分没问题,运行代码报错,见下
☐ Wrong:
☐ 错误:
user@pc:~$scala Game.scala
/home/$USER/.../src/ul/org/bloxorz/Game.scala:1: error: illegal start of definition
package ul.org.bloxorz
Scala code should be invoked from terminal pretty much the same as Java code (you should give it a fully qualified class name and not the file name like I did in first example)
Scala 代码应该像 Java 代码一样从终端调用(你应该给它一个完全限定的类名,而不是我在第一个例子中所做的文件名)
☑ Correct:
☑ 正确:
user@pc:~$scala ul.org.bloxorz.Game
回答by Daniel C. Sobral
I don't get this error. How are you compiling this? And, by the way, what web site? As for REPL, it doesn't accept packages. Packages are only for compiled code.
我没有收到这个错误。你怎么编译这个?顺便说一下,什么网站?至于 REPL,它不接受包。包仅用于已编译的代码。

