public
Last active

When is a tuple not a tuple, and other scala type error ambiguities

  • Download Gist
HigherOrderVsCurried.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
scala> def k( j : (Int => Int) => Int ) : Int = j(_+1)
k: (j: Int => Int => Int)Int
 
scala> def p(q : Int => Int) : Int = q(0)
p: (q: Int => Int)Int
 
scala> def z = (x : Int) => (y : Int) => x + y
z: Int => Int => Int
 
scala> k(p)
res1: Int = 1
 
scala> k(z)
<console>:10: error: type mismatch;
found : Int => Int => Int
required: Int => Int => Int
k(z)
^
TuplesVsArguments.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
scala> def f(x : Int, y : Int) : Int = x + y
f: (x: Int, y: Int)Int
 
scala> def g(z : (Int, Int)) : Int = z._1 + z._2
g: (z: (Int, Int))Int
 
scala> def h(c : (Int, Int) => Int) : Int = c(1,2)
h: (c: (Int, Int) => Int)Int
 
scala> f(1,2)
res0: Int = 3
 
scala> g(1,2)
res1: Int = 3
 
scala> h(f)
res2: Int = 8
 
scala> h(g)
<console>:10: error: type mismatch;
found : (Int, Int) => Int
required: (Int, Int) => Int
h(g)
^

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.