Create a generic class (with wildcard types) and multiple constructors?

Not entirely certain what you're trying to do, but you can do it like this: import java.lang. Comparable class MyComparableT :Tb,Tb()(implicit ev: T=:=Tb) { def this(a: T)(implicit ev: T=:=Tb) = { this() println(a) } }.

Thanks! I'm still quite unused to Scala's constructors, and multiple parameters list, so I haven't thought before of duplicating the implicit parameter down on the other constructor (you know... being implicit deceived me into thinking it wasn't needed) still: the Java equivalent is more convenient: I can instantiate the Scala MyComparable like this: new MyComparableString,String() new MyComparableString,String("dummy") – berdario Mar 17 at 17:50 while the Java MyComparable can be used like this: new MyComparable() new MyComparable("dummy") new MyComparableString() new MyComparableString("dummy") Thus, for now I'll upvote the answer and wait to see if there're better solutions (It's my first question here on SO, maybe it's quite superfluous to state this) edit: apparently I can't upvote until I'll have 15 reputation :/ – berdario Mar 17 at 17:52 I accepted your answer for these reasons: - I don't like to accept my own answer - Your answer is the closest to my original approach - I really need the +2 rep for adding extra links to my answer (ugh: I hate this site) – berdario Apr 4 at 22:34.

You are over-complicating the issue -- though it would be nice if Java had declaration-site variance, which would make all of this moot. Anyway, here's the equivalent code: class MyComparableT : T() { def this(a: T) = { this() println(a) } } Granted that this does not use raw type, and has two type parameters instead of one. Then again, there's the question of what you are actually trying to accomplish with that declaration.

I wonder if what you actually want isn't this: import scala.annotation.unchecked. UncheckedVariance class MyComparable-T trait Cp-T { | def compareTo(other: T): Int | } defined trait Cp scala> class MyComparable-T.

Thanks, your first proposal is very simple, though it forces us to explicitly specify the T2 type of which our T is a subtype. The second proposal is Interesting, but I tried in my case and indeed it doesn't help... I pondered on it quite a bit, and I think that exploting contravariance doesn't help here: the problem is with the type that's passed to the constructor; having a contravariant parameter might help only when fetching that type from the class, or reassigning the object to some other sub/superclass – berdario Apr 4 at 22:53 Actually, before posting this question, I didn't even know about the meaning of covariance and contravariance in scala, but since I've the burden of working with plain old java invariants, my options were already quite limited. Your third example is neat and (together with snippet #2) prompted me to learn how does variance work in scala, but it's still unusable in this case: my class has to be able to accept existing java types that can't implement a Cp scala trait (on second thought, I could subclass all the needed classes and mixin this trait for each one, but it seems quite dirty this way) – berdario Apr 4 at 23:05.

This still isn't the same as the Java code I posted, but it seems to be close enough and cleaner than the other solutions: import java.lang. Comparable class MyComparableT : T) = { this() println(a) } } Here's a bunch of miscellaneous links I read since when I got stuck with this problem, that may help someone else with a similar problem: When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate? To understand what does =:= really do programming-scala.labs.oreilly.com/ch12.... scala-lang.org/node/129 Type erasure and other low-level details of the Java generics scala-lang.org/node/124 http://scalada.blogspot.com/2008/01/existential-types.html http://lamp.epfl.ch/~emir/bqbase/2007/06/13/existentials.html.

Very nice! I would also go with this one. – Felix Apr 5 at 7:28.

In Scala, a subclass can only call one of it's superclass's constructors. How can I work around this rule? Assume the Java class is legacy code that I can't change.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions