“错误:'='预期,但'=>'找到。”来自sbt控制台的错误

时间:2013-04-03 17:38:56

标签: scala sbt

我想使用sbt控制台作为REPL来尝试一些scala代码。

这就是我启动sbt console的方式

$ sbt
[info] Loading project definition from /Users/antkong/wd/scala/recfun/project/project
[info] Loading project definition from /Users/antkong/wd/scala/recfun/project
[info] Set current project to progfun-recfun (in build file:/Users/antkong/wd/scala/recfun/)
> console
[info] Compiling 2 Scala sources to /Users/antkong/wd/scala/recfun/target/scala-2.10/classes...
[info] 'compiler-interface' not yet compiled for Scala 2.10.1. Compiling...
[info]   Compilation completed in 22.682 s
[info] Starting scala interpreter...
[info] 

然后我输入了这段代码:

def sq(x:Int)=>x*x

我原以为它是一个有效的scala代码段。所以我不明白为什么sbt会抛出这个错误信息:

scala> def sq(x:Int)=> x*x
<console>:1: error: '=' expected but '=>' found.
       def sq(x:Int)=> x*x
                    ^

是语法问题还是sbt控制台/ scala解释器的行为?

2 个答案:

答案 0 :(得分:4)

它不是有效的Scala代码。有效是

def sq(x : Int) = x*x

def sq = (x : Int) => x*x

val sq = (x : Int) => x*x

第二个定义了一个返回函数的方法。第三个定义了函数类型的值。所有这三个都可以按照以下方式使用

sq(2)

答案 1 :(得分:2)

  

=&GT;' (按姓名打电话)正是我想要尝试的。

在这种情况下,您需要在参数上使用它:

scala> def sq(x : => Int) = x*x
sq: (x: => Int)Int

scala> 
相关问题