Scala 给了我“非法的定义开始" [英] Scala giving me "illegal start of definition"

查看:61
本文介绍了Scala 给了我“非法的定义开始"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开始使用 Scala,但无法走出起点.

I'm trying to get started with Scala and cannot get out of the starting gate.

由行组成的文件

package x

给我

错误:定义的非法开始

不管 x 是什么,也不管我把文件放在哪里(我有一个理论,我必须将文件放在目录层次结构中以匹配包定义,但没有).我在使用网站和 REPL 的示例代码时遇到了同样的错误.

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.

推荐答案

看起来您正试图在 Scala 脚本中声明 package 成员资格(使用 scalacode> 命令)或在 REPL 中.

It looks like you're trying to declare the package membership in a Scala script (run using the scala command) or in the REPL.

只有用scalac 编译的仅定义类和对象的文件可以定义为属于包.

Only files defining just classes and objects which are compiled with scalac may be defined as belonging to a package.

当您在脚本或 REPL 会话中运行代码时,在幕后它实际上是在对象的方法内编译的,在该范围内,包声明是不合法的.

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.

这篇关于Scala 给了我“非法的定义开始"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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