I defined two functions(method) in Scala REPL:
scala> val b=(x:Int)=>x+1
b: Int => Int = <function1>
scala> def c(x:Int)=x+1
c: (x: Int)Int
And the usage:
scala> b(1)
res4: Int = 2
scala> c(1)
res5: Int = 2
While both definition works, it seems that b
and c
have different type. And I was wondering whether there are some differences between them. Why doesn't Scala use the same type for b
and c
? Does anyone have ideas about this?
Not duplicate:
This question is not a duplicate of the linked question. Even though it asks about the difference between using def and val to define a function, the code example makes it clear that the asker is confused about the difference between methods and functions in Scala. The example doesn't use a def to define a function at all. – Aaron Novstrup 7 hours ago
def
andval
to define a function, the code example makes it clear that the asker is confused about the difference between methods and functions in Scala. The example doesn't use adef
to define a function at all. – Aaron Novstrup