2.9.x type inference bug workaround

15 views
Skip to first unread message

Ryan LeCompte

unread,
May 20, 2013, 7:59:41 PM5/20/13
to scala-user
The following breaks on Scala 2.9.x, but works on 2.10+:

scala> def foo[A, C <: Traversable[A]](c: C) { println(c) }

scala> foo(Map.empty[String, Int])
<console>:9: error: inferred type arguments [Nothing,scala.collection.immutable.Map[String,Int]] do not conform to method foo's type parameter bounds [A,C <: Traversable[A]]
              foo(Map.empty[String, Int])

Someone told me about the following workaround, which works on Scala 2.9.x:

scala> def foo[A, C <: Traversable[A]](c: C with Traversable[A]) { println(c) }

scala> foo(Map.empty[String, Int])
Map()

It also seems to work for other types such as strings, BitSets, etc. Is there a type that will not work with this 2.9.x workaround? I know that IsTraversableLike/etc fixes this issue in 2.10+.

Thanks,
Ryan

Miles Sabin

unread,
May 21, 2013, 2:51:43 AM5/21/13
to Ryan LeCompte, scala-user

On 21 May 2013 00:59, "Ryan LeCompte" <leco...@gmail.com> wrote:
> I know that IsTraversableLike/etc fixes this issue in 2.10+.

IsTraversableLike (or a very slight variation upon it) will work just fine on 2.9.x, so I'd recommend backporting that to your project.

Cheers,

Miles

Reply all
Reply to author
Forward
0 new messages