Working around type erasure ambiguities (Scala)

14 06 2010

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)
About these ads

Actions

Information

2 responses

15 06 2010
Tweets that mention Working around type erasure ambiguities (Scala) « Michid’s Weblog -- Topsy.com

[…] 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 […]

24 07 2010
Michel

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: