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

What I was thinking was that in the first implementation I would take the function f and pass in an A and an anything (but the compiler will type check that the second param is a B) to get a C out.

1
  • just check the generated bzte code Commented Jul 8, 2014 at 21:42

1 Answer 1

2

Yes, they are identical. If you compile:

object Test { def curry[A,B,C](f: (A, B) => C): A => (B => C) = (a: A) => f(a, _) def curry2[A,B,C](f: (A, B) => C): A => (B => C) = (a: A) => (b: B) => f(a, b) } 

with -Xprint:typer, you get the intermediate abstract syntax tree:

[[syntax trees at end of typer]] package <empty> { object Test extends scala.AnyRef { def <init>(): Test.type = { Test.super.<init>(); () }; def curry[A, B, C](f: (A, B) => C): A => (B => C) = ((a: A) => ((x$1: B) => f.apply(a, x$1))); def curry2[A, B, C](f: (A, B) => C): A => (B => C) = ((a: A) => ((b: B) => f.apply(a, b))) } } 

During the "typer" stage, when the compiler assigns types to everything, it realizes that the _ (now named x$1) must be type B.

Sign up to request clarification or add additional context in comments.

1 Comment

Also :javap -v Test$ in the REPL shows that byte-code is identical

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.