Unification of a general type: several parameters (T, T) compared with several parameter lists (T) (T)?

I'm a little confused by the difference in type checking between cardinal and unpainted generic functions:

scala> def x[T](a: T, b: T) = (a == b)
x: [T](a: T, b: T)Boolean
scala> def y[T](a: T)(b: T) = (a == b)
y: [T](a: T)(b: T)Boolean

My intuition was that the two x(1, "one"), and y(1)("one")should lead to the type of error, but I was wrong:

scala> x(1, "one")
res71: Boolean = false
scala> y(1)("one")
<console>:9: error: type mismatch;
 found   : java.lang.String("one")
 required: Int
              y(1)("one")
                   ^

At first, I thought some kind of implicit casting was going on, but that doesn't seem to be the case:

scala> x(1 :Int, "one" :String)
res73: Boolean = false

So what is going on? What should be my intuition?

+5
source share
2 answers

I think that in the first case it is a downcasting of both arguments, such as T: Any. In the second, it is currying for Int, and then crashing into String.

This seems to bear me:

scala> y(1)_
res1: Int => Boolean = <function1>
+9
source

Scala . , :

def x[T](a: T, b: T)(c: T) = (a == b)
scala> x(1, "one") _
res0: Any => Boolean = <function1>

, Int String Any ( == Any).

, , :

def y[T,U](a: T)(b: U)(c: (T,U)) = (a == b)
scala> y(1)("one")
res1: (Int, java.lang.String) => Boolean = <function1>

!

def z[T,U](a: T)(b: U)(c: (T,U) = (a,b)) = (c._1 == c._2)
scala> z(1)("one")()
res2: Boolean = false

, , ( ).

+10

All Articles