Anthony Kong
Anthony Kong

Reputation: 40814

"error: '=' expected but '=>' found." error from sbt concole

I want to use sbt console as a REPL to try out some scala code.

This is how I started up 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] 

Then I typed in this code:

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

I have expected it is a valid scala code snippet. So I do not understand why sbt throws out this error message instead:

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

Is it a syntax issue or just a behaviour of sbt console/scala interpreter?

Upvotes: 0

Views: 884

Answers (2)

David Apltauer
David Apltauer

Reputation: 983

It is not a valid Scala code. Valid is

def sq(x : Int) = x*x

or

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

or

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

The second one defines a method which returns a function. The third one defines a value of type function. All three can be used following way

sq(2)

Upvotes: 4

us2012
us2012

Reputation: 16263

'=>' (call by name) is exactly what I want to try.

In that case, you need to use it on the parameters:

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

scala> 

Upvotes: 2

Related Questions