zell
zell

Reputation: 10204

What is wrong with def f=x:Int->List(x-1,x,x+1) in Scala?

Hi I do not quite follow why this definition in Scala does not work.

scala> def f=x:Int->List(x-1,x,x+1)
<console>:1: error: ';' expected but '(' found.
       def f=x:Int->List(x-1,x,x+1)
                        ^

Upvotes: 1

Views: 49

Answers (2)

Tim
Tim

Reputation: 27356

There are two immediate problems:

  1. -> should be =>
  2. x:Int should be (x:Int)

So this works:

def f = (x: Int) => List(x - 1, x, x + 1)

However this is defining a function that returns a function, which is probably not what is intended. So instead try

val f = (x: Int) => List(x - 1, x, x + 1)

or

def f(x: Int) = List(x - 1, x, x + 1)

Upvotes: 4

Alejandro Alcalde
Alejandro Alcalde

Reputation: 6220

That is syntactically incorrect: I think what you want is:

def f(x: Int):List[Int] = List(x-1,x,x+1)

First, A function type is defined with =>, not ->, maybe you come from haskell, so for example, a function with Int param and that returns a List[Int] will have the type Int => List[Int]

Upvotes: 2

Related Questions