Higher-order functions with tuples

I have the following problem. I tried to create a high-order function that takes two parameters: String and the type of the function. The type of function is defined as follows:

(String, List[String]) => List[(String, List[String])] 

I also defined two functions f1 and f2 that are of the same type. Subsequently, I try to call myfun using f1 or f2 . Here is the code:

 object Main extends App { def f1(t: (String,List[String])): List[(String,List[String])] = ... def f2(t: (String,List[String])): List[(String,List[String])] = ... def myfun(tableName: String)(fn: (String,List[String]) => List[(String,List[String])]): List[(String,List[String])] = ... val res: List[(String,List[String])] = myfun("...")(f1) res foreach println val res2: List[(String,List[String])] = myfun("...")(f2) res2 foreach println } 

and here is the error:

 [error] found : (String, List[String]) => List[(String, List[String])] [error] required: (String, List[String]) => List[(String, List[String])] [error] val res: List[(String,List[String])] = myfun("...")(f1) 

I cannot understand why the compiler complains. Can someone explain this?

+7
source share
1 answer

Try it. Note the extra set of parentheses surrounding the argument list on fn .

 def myfun(tableName: String) (fn: ((String,List[String])) => List[(String,List[String])]): List[(String,List[String])] = ... 

Unfortunately, this extra set of parentheses is needed to distinguish

 Function1[(String, List[String]), List[(String,List[String])]] 

of

 Function2[String, List[String], List[(String, List[String])]] 
+8
source

All Articles