Injured by Type-Erasure

25 views
Skip to first unread message

Seyed H. HAERI (Hossein)

unread,
Jun 4, 2012, 7:00:48 AM6/4/12
to scala-user
Dear all,

In a class X of mine, I'd like to have two overloads of a method f like

class X {
class Y(X, I)

def f(is: I*) = Y(this, is: _*)
def f(vs: V*) = Y(this, (for(v <- vs) yield v.x): _*) //line 53
}

but, the compiler emits

double definition: method f:(vs: V*)X.this.Y and method f:(is:
I*)X.this.Y at line 53 have same type after erasure: (vs: Seq)

The error message is clear enough for me to understand what's going
on. Is there any hope, though, that I can rescue my code from the
nasty type-erasure occurring here?

TIA,
--Hossein

--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------

Stephen Compall

unread,
Jun 4, 2012, 7:55:47 AM6/4/12
to Seyed H. HAERI (Hossein), scala-user
On Jun 4, 2012, at 7:00 AM, "Seyed H. HAERI (Hossein)" <hossei...@gmail.com> wrote:

> class X {
> class Y(X, I)
>
> def f(is: I*) = Y(this, is: _*)
> def f(vs: V*) = Y(this, (for(v <- vs) yield v.x): _*) //line 53
> }
>
> double definition: method f:(vs: V*)X.this.Y and method f:(is:
> I*)X.this.Y at line 53 have same type after erasure: (vs: Seq)

Require one argument. To put it another way, what does f() mean?

--
Stephen Compall
Greetings from sunny Appleton!

Sonnenschein

unread,
Jun 4, 2012, 1:22:38 PM6/4/12
to scala-user
>   class Y(X, I)

Is that Scala?

missingfaktor

unread,
Jun 4, 2012, 3:06:50 PM6/4/12
to Seyed H. HAERI (Hossein), scala-user
You should in general avoid method overloading in Scala. See this thread:  http://stackoverflow.com/questions/2510108/why-avoid-method-overloading.

Now, if you really want to do it, here is a little trick: Add some dummy implicit arguments to methods.

implicit object Dummy1
implicit object Dummy2

def f(is: I*)(implicit d: Dummy1.type) = whatever
def f(vs: V*)(implicit d: Dummy2.type) = whatever

Since the arguments are implicit, you don't have to pass them at call site.
--
Cheers,

Sonnenschein

unread,
Jun 4, 2012, 4:00:12 PM6/4/12
to scala-user
> You should in general avoid method overloading in Scala.

Imo, method overloading is a legitimate feature. I'd encourage you to
use it whenever different method names would frustrate users. Also,
apply and unapply are quite frequently overloaded...

Though we have no idea what 'f' should express in the above
example....

Peter
Reply all
Reply to author
Forward
0 new messages