0
votes

I can't understand. why each function have different return type using type constraint or not.

First i did make function inv like below, and it' return type is Int

implicit val x = 10
def inv[A](a: A)(implicit ev: Int) = ev
inv(1) // it return 10

// res0: Int = 10

Then i did make function inv2 with type constraints like below, and it' return type is Function1

def inv2[A](a: A)(implicit ev: Int <:< Int) = ev
inv2(1)

// res2: <:<[Int,Int] = <function1>
1

1 Answers

4
votes

inv2's return type is inferred as <:<[Int,Int] - as expected.

However, the runtime type of the returned value is Int => Int. That's because <:< is a function.

sealed abstract class <:<[-From, +To] extends (From => To) with Serializable

And calling toString on a function results in <function1> being printed.