Reputation: 53786
For below sum method :
def sum(term: (Int) => Int , next: (Int) => Int) : Int = {
0
}
How can I access the Int parameter of function term
If I try :
def sum(term: (param : Int) => Int , next: (Int) => Int) : Int = {
0
}
I receive error :
Multiple markers at this line - only classes can have declared but undefined members - not found: type param -
')' expected but ':' found.
Upvotes: 1
Views: 113
Reputation: 15773
term: (Int) => Int
simply tells the compiler that you are passing in a function that takes a a Int
parameter and returns another Int
, there's no need to bind the variable:
scala> def sum(term: (Int) => Int) : Int = {
| term(10)
| }
sum: (term: Int => Int)Int
scala> def useSum() = {
| sum {
| someInt =>
| someInt + 1
| }
| }
useSum: ()Int
scala> useSum
res6: Int = 11
You could also curry the sum
function and pass in the parameter you want to apply to the function you are passing:
def sum(someInt: Int)(term: (Int) => Int) : Int = {
term(someInt)
}
def useSum(someInt: Int) = {
sum(someInt) {
someInt =>
someInt + 1
}
}
Upvotes: 1
Reputation: 9158
You can't, as when sum
is applied, passed term
function is not yet called. BTW there is a syntax error in the second code, as parameters of lambda can't be named in this way.
Upvotes: 0