scala - Declaring functions two ways. What is the distinction? -
are these 2 function declarations different?
if not, why have different tostring values?
scala> def f: (int) => int = x=> x*x f: (int) => int scala> def f(x: int) = x*x f: (int)int
the first no-argument method f1
returns function1[int, int]
.
scala> def f1: (int => int) = (x: int) => x * x f1: (int) => int
the second 1 argument method f2
takes an int
, returns int
.
scala> def f2(x: int): int = x * x f2: (x: int)int
you can invoke f1 , f2 same syntax, although when call f1(2)
expanded f1.apply(2)
.
scala> f1 res0: (int) => int = <function1> scala> f1(2) res1: int = 4 scala> f1.apply(2) res2: int = 2 scala> f2(2) res3: int = 4
finally, can 'lift' method f2
function follows.
scala> f2 <console>:6: error: missing arguments method f2 in object $iw; follow method `_' if want treat partially applied funct ion f2 ^ scala> f2 _ res7: (int) => int = <function1> scala> (f2 _).apply(2) res8: int = 4
exercise: type of f1 _
?
Comments
Post a Comment