Vangogh500
Vangogh500

Reputation: 959

Tuple inference fails

Hi so I have a trait with generics +T and +U.

def test[I >: T, V >: U](e: Traversable[I], f: V)
def test[I >: T, V >: U](e: (Traversable[I], V))
def test[I >: T, V >: U](e: Tuple2[Traversable[I], V])

The first one works fine. The last 2 however both spit out:

type mismatch;
[error]  found   : (String, Int)
[error]  required: (Traversable[?], ?)

Why can scala not infer the types in the 2nd and 3rd examples? What am I missing?

Thanks

Upvotes: 3

Views: 86

Answers (2)

Dmytro Mitin
Dmytro Mitin

Reputation: 51658

This code compiles in Scala 2.12.3 without errors:

trait MyTrait[+T, +U] {
  def test[I >: T, V >: U](e: Traversable[I], f: V)
  def test1[I >: T, V >: U](e: (Traversable[I], V))
  def test2[I >: T, V >: U](e: Tuple2[Traversable[I], V])
}

Upvotes: 0

Ziyang Liu
Ziyang Liu

Reputation: 810

This looks like a compiler bug to me. I don't see a reason why it can convert String to Traversable in the first case but not the other two cases.

Upvotes: 1

Related Questions