In my previous post I showed a workaround for the type erasure ambiguity problem in Java. The solution uses vararg parameters for disambiguation. As Paul Phillips points out in his comment, this solution doesn’t directly port over to Scala. Java uses Array to pass varargs, Scala uses Seq. Unlike Array, Seq is not reified so Seq[String] and Seq[Int] again erase to the same type putting us back to square one.
However, there is another way to add disambiguation parameters to the methods: implicits! Here is how:
implicit val x: Int = 0 def foo(a: List[Int])(implicit ignore: Int) { } implicit val y = "" def foo(a: List[String])(implicit ignore: String) { } foo(1::2::Nil) foo("a"::"b"::Nil)
[…] This post was mentioned on Twitter by Planet Scala, Bubbl Scala Feed. Bubbl Scala Feed said: Working around type erasure ambiguities (Scala) « Michid’s Weblog http://ff.im/-m43vi […]
There is a discussion on Stackoverflow were I gratefully used your tip! Thanks!
The interesting thing is that others also produced their own solutions which seem to work as well:
http://stackoverflow.com/questions/3307427/scala-double-definition-2-methods-have-the-same-type-erasure