This compiles fine:
> class A {
> def m(x: Int) = "x is: "+ x
> def m(x: Double) = "x is: "+ x
> }
But this fails because the overload disappears in the presence of type erasure:
> class B {
> def m(x: Option[Int]) = "x is: "+ x
> def m(x: Option[Double]) = "x is: "+ x
> }
Any suggestions on good ways to work around this?
Why do I need to do this? I'm working on making Borachio (Scala mocking library) type safe. I'm pretty much there, except when dealing with overloaded methods. The problem is that mock methods, instead of taking the "raw" parameter types, take a MockParameter. So:
> def foo(x: Double)
becomes (in the mock object):
> def foo(x: MockParameter[Double])
Which means that as well as specifying specific values, e.g:
> m.expects.foo(1.23)
Wildcards and epsilon matches can also be used, e.g:
> m.expects.foo(*)
> m.expects.foo(~1.23)
This is all working just fine for non-overloaded methods. But for overloaded methods, I hit the type erasure problem above :-(
Suggestions gratefully received!
--
paul.butcher->msgCount++
Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?
http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher
If you're generating code, you can generate subtypes of those types,
though I'm not sure that would solve your particular problem. But for
example:
class DoubleMockParameter extends MockParameter[Double]
class IntMockParameter extends MockParameter[Int]
You can overload those two subtypes just fine.
Bill
--
Bill Venners
Artima, Inc.
http://www.artima.com
Don't use all your hatred up on type erasure, save some of it for
overloading...!
A lot of it, even.
--
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
Unfortunately, given that the language supports the feature, I have little choice but to also do so, irrespective of my feelings about it.
(although, TBH, it's not something that I feel strongly about either way).
Thanks Debasish,I'm going to have to stare at that for a while to understand how it works, but it looks good :-)
On 28 Jun 2011, at 07:22, Debasish Ghosh wrote:One approach to resolve such overloads is to use typeclasses ..import B._class B {def m[A: BP](x: Option[A]) = implicitly[BP[A]] m x}object B {sealed trait BP[A] {def m(x: Option[A])}implicit object BPInt extends BP[Int] {def m(x: Option[Int]) = println("x is: " + x)}implicit object BPDouble extends BP[Double] {def m(x: Option[Double]) = println("x is: " + x)}}
val b = new Bb.m(Some(10))b.m(Some(10.toDouble))HTH.
def m[A: BP](x: Option[A]) = ...
def m[A](x: Option[A])(implicit ev$1: BP[A]) = ...
def m[A: BP](x: Option[A]) = implicitly[BP[A]] m x
def m[A](x: Option[A])(implicit bp: BP[A]) = bp.m(x)
Thanks, Kevin - that's very helpful indeed.
Possibly a silly question, but is there any significance to the choice of names (i.e. "B" and "BP")?
Best regards Norbert Tausch
Is type erasure only a point to the JVM? Has .Net the same mechanism or problem?
It seems that - although there are workarounds - type erasure makes Scala code more complicated.
Is there any chance that Scala can overcome this 'limitation' in future?
Best regards Norbert Tausch
Am 28.06.2011 13:41, schrieb Debasish Ghosh:Actually I intended to make it BParam (parameter for B) .. but laziness took over :-) and now that u mention it I think it should have been MP (parameter for method m) .. but anyway ..
On Tue, Jun 28, 2011 at 5:08 PM, Paul Butcher <pa...@paulbutcher.com> wrote:
On 28 Jun 2011, at 10:09, Kevin Wright wrote:Thanks, Kevin - that's very helpful indeed.
> Okay, this should help get you going...
Possibly a silly question, but is there any significance to the choice of names (i.e. "B" and "BP")?
--
paul.butcher->msgCount++
Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?
http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher
--
Debasish Ghosh
http://manning.com/ghosh
Twttr: @debasishg
Blog: http://debasishg.blogspot.com
Code: http://github.com/debasishg
Is type erasure only a point to the JVM? Has .Net the same mechanism or problem?
It seems that - although there are workarounds - type erasure makes Scala code more complicated.
Is there any chance that Scala can overcome this 'limitation' in future?
Best regards Norbert Tausch
Am 28.06.2011 13:41, schrieb Debasish Ghosh:Actually I intended to make it BParam (parameter for B) .. but laziness took over :-) and now that u mention it I think it should have been MP (parameter for method m) .. but anyway ..
On Tue, Jun 28, 2011 at 5:08 PM, Paul Butcher <pa...@paulbutcher.com> wrote:
On 28 Jun 2011, at 10:09, Kevin Wright wrote:Thanks, Kevin - that's very helpful indeed.
> Okay, this should help get you going...
Possibly a silly question, but is there any significance to the choice of names (i.e. "B" and "BP")?
--
paul.butcher->msgCount++
Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?
http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher
--
Debasish Ghosh
http://manning.com/ghosh
Twttr: @debasishg
Blog: http://debasishg.blogspot.com
Code: http://github.com/debasishg
Best regards Norbert Tausch
<http://gafter.blogspot.com/2006/12/super-type-tokens.html>
Although I haven't employed it in Scala yet (no need at the moment), I
think it could help in certain circumstances.
> I could get to hate type erasure with a fiery passion :-(
>
> This compiles fine:
>
>> class A {
>> def m(x: Int) = "x is: "+ x
>> def m(x: Double) = "x is: "+ x
>> }
>
> But this fails because the overload disappears in the presence of type
> erasure:
>
>> class B {
>> def m(x: Option[Int]) = "x is: "+ x
>> def m(x: Option[Double]) = "x is: "+ x
>> }
Like this:
class B{
def m(x: Option[Int]) = "x is: "+x
def m(x: Option[Int])(implicit sentinel:DummyImplicit) = "x is: "+x
}
If you have other overloads, just make sure they have different numbers
of DummyImplicit parameters. DummyImplicit is defined in Predef, so it's
always available.
--Ken
--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
wow. i don't know jack about making or fixing languages, but i sure
hope somebody can come up with a better alternative some day :-}
On the other hand, if Sun had designed reified generics in the JVM,
we'd probably stuck with a version of reified generics that didn't
allow covariance and contravariance. So count your lucky stars that
the type erasure problem here can actually be resolved pretty easily
with a relatively unintrusive hack.
--
Fabio Cechinel Veronez
What this all means is that if you want to add reified generics to the JVM, you should be very certain that that implementation can encompass both all static languages that want to do innovation in their own version of generics, and all dynamic languages that want to create a good implementation and a nice interfacing facility with Java libraries. Because if you add reified generics that doesn’t fulfill these criteria, you will stifle innovation and make it that much harder to use the JVM as a multi language VM.