When do I have to treat my methods as partially applied functions in Scala?

后端 未结 3 458
暖寄归人
暖寄归人 2020-12-15 04:03

I noticed that when I\'m working with functions that expect other functions as parameters, I can sometimes do this:

someFunction(firstParam,anotherFunction)
         


        
3条回答
  •  萌比男神i
    2020-12-15 04:34

    The rule is actually simple: you have to write the _ whenever the compiler is not explicitly expecting a Function object.

    Example in the REPL:

    scala> def f(i: Int) = i    
    f: (i: Int)Int
    
    scala> val g = f
    :6: error: missing arguments for method f in object $iw;
    follow this method with `_' if you want to treat it as a partially applied function
           val g = f
                   ^
    
    scala> val g: Int => Int = f  
    g: (Int) => Int = 
    

提交回复
热议问题