Why isn't Tuple1[T] =:= T?

Why isn’t Tuple1[T] isn’t =:= T?

Doesn’t it make easy to handle type arguments in match types? For example, you would no longer need to write the last case in the following?


type WithEnv[D, R] = D match 
  case x *: xs => x ?=> WithEnv[xs, R]
  case EmptyTuple => R
  case _ => D ?=> R

This example you have given already does not require the final case:

type WithEnv[D, R] = D match 
  case x *: xs => x ?=> WithEnv[xs, R]
  case EmptyTuple => R

scala> def foo: WithEnv[Tuple1[Int], String] = i ?=> i.toString
def foo: (Int) ?=> WithEnv[scala.Tuple$package.EmptyTuple.type, String]
1 Like

Tuple1[T] =!= T for the same reason that List(x) != x.

If it would be the case that Tuple1[T] =:= T, then T <:< Tuple1[T] would also be true (obviously). Since Tuple1[T] <:< Product, that means that T <:< Product for any T, including Any!

Also Tuple2[A, B] would be a subtype of (even equivalent to) Tuple1[Tuple2[A, B]]. That would mean that your WithEnv[(A, B), R] would non-deterministically choose to interpret (A, B) as either

  • a 2-ary tuple with A and B, resulting in A ?=> B ?=> R, or
  • a 1-ary tuple with (A, B), resulting in (A, B) ?=> R.

I will let you imagine what more can go wrong from here. :wink:

8 Likes