3

Being relatively new at Scala, I was playing around with partially-applied function syntax on sbt console. I ran into a very weird issue in which I do not understand Scala's behavior.

The example is contrived and would unlikely be encountered in reality. That said, here it is. Suppose I define the following function:

scala> def f(x: Int, y: Int) = "%d %d".format(x, y)

Now if I type in

scala> f(1, _:Int)(2)
res: Int => Char = <function1>

The result is a Int => Char function, which is very unusual. In other words, Scala is (temporarily) treating f(1, _:Int) as a String (vs. its actual type: Int => String) when applying the parameter (i.e., 2).

If, instead, parentheses are used, what I expect to happen occurs:

scala> (f(1, _:Int))(2)
res: String = 1 2

However, this does not appear to be an order-of-operations issue, as I cannot find a way to add parentheses to achieve the unexpected behavior (i.e., having the result be of type Int => Char).

Any ideas?

1
  • This is surprising to me, and seems at odds with my reading of section 6.23 of the language specification, which says that an underscore is bound by the smallest expression that contains it. Oct 3, 2013 at 21:07

1 Answer 1

2

At first about the result type of:

scala> f(1, _:Int)(2)
res: Int => Char = <function1>

Check this out:

scala> val str = "hello"
str: String = hello

scala> str(2)
res12: Char = l

I think this is clear.

No to the function itself, as it also easy. When you are lifting it to a function with an underscore you are lifting not only f, but also with a call (2) on a string (first result), which is why you get:

res: Int => Char = <function1>

Added

More explicit version. You function f is of type (Int, Int) => String, when you write f(1, _: Int), you are partially applying it to the argument one and returning a function of type Int => String, where Int is the second argument. Then your argument (2) call apply method on the result string from the function Int => String which returns you a Char, from here you get function of type Int => Char, where Int is a second argument to your f function and Char is a char from the resulting string

In the second case, where you have:

scala> (f(1, _:Int))(2)
res: String = 1 2

by parenthesis you are spliting this into to things, the first one is a function Int => String and (2) calls this function, you are passing an argument 2 into this function Int => String:

val ff = f(1, _: Int)
val res = ff(2)
5
  • So I understand why, if f(1, _:Int) were a String, f(1, _:Int)(2) would be a Char. However, what I don't understand is why f(1, _:Int) is being treated as a String to begin with, when its type is Int => Char.
    – bacchuswng
    Oct 3, 2013 at 20:59
  • @bacchuswng You wrote it yourself: def f(x: Int, y: Int) = "%d %d".format(x, y) f takes two ints and returns a string
    – 4lex1v
    Oct 3, 2013 at 21:00
  • @bacchuswng i've updated the answer to be more clearer in this moment
    – 4lex1v
    Oct 3, 2013 at 21:06
  • It still seems counterintuitive to me why, when calling apply(2) on the result of f(1, _: Int), Scala treats this as an apply(2) on the String result of applying f(1, _: Int) vs. the Int => String result of f(1, _: Int) itself. Perhaps I just need to accept this as how Scala is :).
    – bacchuswng
    Oct 3, 2013 at 22:32
  • @bacchuswng cause we can think of f(1, _:Int).apply in term of curried function of type Int => String => Char, that's how currying works. If you wrap it in brackets (f(1, _:Int))(2) this is not a curried function, first you get Int => String and then pass 2 as an argument
    – 4lex1v
    Oct 3, 2013 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.