Ben Knoble Ben Knoble - 3 months ago 16
Scala Question

Scala Higher Order Functions and Implicit Typing

I recently started working with Functional Programming in Scala and am learning Scala in the process. While attempting one of the Chapter 2 exercises to define a function that curries another function, I ran into this:

If I write

def curry[A,B,C](f: (A,B) => C): A => B => C =
a: A => b: B => f(a, b)


then I get


Chapter2.scala:49: error: ';' expected but ':' found.

a: A => b: B => f(a, b)

_______^

one error found


BUT if I write

def curry[A,B,C](f: (A,B) => C): A => B => C =
a => b => f(a, b)


then it compiles fine, with no warnings, and works. What's the difference?

Answer

You just need to enclose your variables in parentheses. In your example, you can write:

def curry[A,B,C](f: (A,B) => C): A => B => C =
  (a: A) => (b: B) => f(a, b)