Reputation: 10204
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
Reputation: 27356
There are two immediate problems:
->
should be =>
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
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