Strange definition and call of a function with curly bracket in Scala (function as parameter of a function)

Viewed 310

I'm totally new with Scala, I have to maintain an old code, so I have to understand what it does.
Now I am stuck on this piece of code, it is about defining and calling a method.

This is the definition of the method:

private def myMethod[I, O](price: Long, id: Int)(i: I)(f: (I, String) => O): O = {
      ..some code..
}

this is the method call

myMethod(price, id)(b) {
      ..some code.. //single line of code, just calling an other function 
}

I understood the part of having type parameter also of having multiple parameter (currying).
But what I didn't understand, is :

  • first of all this part: (f: (I, String) => O) , this is completely strange for me
  • second, why in the method call, it contains code after the { symbol, is it overriding the original method? even it's the case, it make no sense to override it when making the call
  • also, myMethod is supposed to return a value of type O , but in my code it's never affected to any variable. (EDIT: this point is clear now, I just misunderstood the code, nvm mind about it)

Please can any one clarify this points (especially the first and second one which are making me so confused)


EDIT

private var x : classX

myMethod(price, id)(b) {
      x.listX //calling method without parameters
}

def listX (param1: ListFaFBI, param2: String): ListX ={
  //returning an Object of type ListX, not a function
}

as you can see that myMethod is calling listX. if I understood well, myMethod is returning the method listX itself which has two parameters ListFaFBI (or I) and String and returning ListX (or O) as defined in (f: (I, String) => O)

2 Answers
  1. f is a function that takes in an I and a String and returns an O. f: (I, String) => O is syntactic sugar for f: Function2[I, String, O].

  2. The braces act essentially the same as parentheses would, although there are some differences, as they can be treated as blocks (See this question). The code inside the braces is actually a function literal, and it will be passed as f. Also see this question. Here,

myMethod(price, id)(b) { (i, s) =>
      ..some code.. 
}

would be syntactic sugar for

myMethod(price, id)(b)({ (i, s) =>
      ..some code.. 
})
  1. I'm not sure what you mean by "in my code it's never affected to any variable", but I assume that what is returned is either irrelevant or that there is an implied return (in case the call to myMethod is at the end of a block.

first of all this part: (f: (I, String) => O) , this is completely strange for me

It is a function, taking a tuple of two elements of type I and String, and returns an O

why in the method call, it contains code after the { symbol, is it overriding the original method?

Your method is using multiple parameter list, and the syntax of the last parameter group is a block definition that allows to define the function value ((I, String) => O)

For example, if we had a method which takes a function in the same parameter list:

def foo(s: String, f: String => String)

Our implementation would look like this:

foo("hello", {
    s => s + "world"
  }
)

However, if we used a separate parameter group:

def foo(s: String)(f: String => String)

Our implementation look like this:

foo("hello") {
   s => s + "world"
}

Which is more eye pleasing and reads nicer.

myMethod is supposed to return a value of type O , but in my code it's never affected to any variable

If you add the implemention of the method we can better help show you where it is returning a value of type O

Related