D. Ben Knoble
D. Ben Knoble

Reputation: 4663

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?

Upvotes: 0

Views: 110

Answers (1)

jtitusj
jtitusj

Reputation: 3086

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)

Upvotes: 4

Related Questions