_ must follow the method

Simplified, from a more complex program:

scala> type T = (String) => String defined type alias T scala> def f(s: String) = s + " (parsed)" f: (s: String)java.lang.String scala> f _ res0: (String) => java.lang.String = <function1> scala> def g(func: T) = func _ <console>:6: error: _ must follow method; cannot follow (String) => String def g(func: T) = func _ ^ 

I really don't understand why this is not working. What is the difference between a method and something in the form (Type1, Type2 ...) => Type , and what is the correct way to get a partial part from something like that?

+4
source share
4 answers
 scala> def g(func: String => String) = func(_) g: (func: (String) => String)(String) => String 

Bracket all matters. This is one of the hardest things about binding _; it can be used to lift the method to close, and it can be used for partial use, but the two uses are not the same!

+6
source

In Scala, there is a difference between methods and functions. Methods always belong to an object, but functions are objects. Method m can be converted to a function using m _

See The Difference Between a Method and a Function in Scala

+8
source

Is this what you are trying to do?

 scala> def g(func: T) = func g: (func: (String) => String)(String) => String scala> g(f)("test") res8: String = test (parsed) 
+4
source

Source: https://habr.com/ru/post/1343685/


All Articles