Search code examples
scalafunctionprogramming-languages

What does "String => String" notation mean when putting it as the return value of a function in Scala Language?


I have this doubt for example in the following function definition (specifically in the return type "String => String"):

def myFunction(line: String): String => String = {
    _.toLowerCase()
}

Thanks in advance.

Edit: Playing a little bit with the REPL made me finally understand this.


Solution

  • A little time playing around in the Scala REPL demonstrates what's going on.

    Welcome to Scala 2.12.7 (OpenJDK 64-Bit Server VM, Java 10.0.2).
    Type in expressions for evaluation. Or try :help.
    
    scala> def myFunction(line: String): String => String = {
         |     _.toLowerCase()
         | }
    myFunction: (line: String)String => String
    
    scala> myFunction("ABCD")
    res0: String => String = $$Lambda$1148/1409513883@5a0e0886
    
    scala> res0("WxYz")
    res1: String = wxyz
    

    Notice that the 1st passed argument, "ABCD", doesn't do anything. It's the string that gets passed to the returned function, res0, that is transformed.