Hi Edmondo,This isn't possible. The JVM/bytecode representation of classes erases the type of generic parameters, so it wouldn't be able to tell Foo[String] from Foo[Int] appart in your implementation.trait Foo[T] {def foo(t: T)}class Twice extends Foo[String] with Foo[Int] // no good, as Foo[String] and Foo[Int] become Foo[_]The way I achieve this kind of thing is to use type classes and implicits.class Twicetrait Foo[T] {def foo(t: T): Unit}object Foo {implicit stringFoo: Foo[String] = new Foo[String] {def foo(s: String): Unit = println("Got string " + s)}implicit intFoo: Foo[Int] = new Foo[Int] {def foo(i: Int): Unit = println("Got int " + i)}}def twiceHandler[T](twice: Twice, t: T)(implicitly tFoo: Foo[T]): Unit = {println("In twice handler")tFoo.foo(t)}Matthew
--Dr Matthew PocockIntegrative Bioinformatics Group, School of Computing Science, Newcastle Universitymailto: turingate...@gmail.comgchat: turingate...@gmail.comirc.freenode.net: drdozerskype: matthew.pococktel: (0191) 2566550mob: +447535664143
trait Foo[T] { def lala: T }
what would Twice return? the only possible solution would be that T is the common supertype of Int and String, then Twice would be a Foo[Any] and could faithfully return either a String or an Int from `lala`.
i ran into this problem before, too, but when you carefully think about it, the idea of inheriting a trait twice with different type arguments is flawed.
so what is it exactly that you try to achieve with Twice?
best, -sciss-
so `MyObj` == `T` and `T` is a sealed trait with three subtypes? Do items hold only one subtype of `T`, or a mix? and what is `apply` supposed to do?
maybe if you try to formulate _what_ exactly you want to do, in plain words, without the _how_, we can try to find out what is the best _how_?
best, -sciss-
On 12 Jan 2012, at 18:48, Edmondo Porcu wrote:
> Yes it is a design flaw, which I am not able to solve.
>
> The idea is the following:
>
> I have a special collections of item , let's say
>
> class MySpecialCollection(val items:IndexedSeq[MyObj]);
>
> this will support a given method for example apply (t:T) where T<:Something.
>
> However, this T-type depends on the items inside the collection, so I cannot determine it in advance...and since T can be only of three different subclasses.... I ended up doing something like that:
>
> class MySpecialCollectionForT1 {
>
> apply(t:T1) = do smth
> apply(t:T2) = throw new UnsupportedOperationException
> apply(t:T3) = throw new UnsupportedOperationException
>
> }
>
> This is a crappy design, but I couldn't imagine anything better... so If you could help me with that I would be very happy to change it :)
>
> Best Regards
>
>
>
> 2012/1/12 Sciss <con...@sciss.de>
There is a set of people... Each of them has a state: sad or happy.
A sad person can be guided for a city tour only by a SadGuide and a happy person can be guided only by an happy guide.
Let's say now you are a company, what would you like to do? Well... Make tours for group of people, because it's more efficient. So you create your collection of people, PeopleCollection.
If all the people are sad, you can guide all them only with a SadGuide, if they are all happy with a HappyGuide. If they differ, you can't create your PeopleCollection.
I am trying to achieve this behaviour... Now imagine to add another layer.
You have a office which groups the people , and you have to decide how many sad tours you have to organize and how many happy tours. You receive a collection of PeopleCollection from the other office, and you want to divide them. You try to collect and because of pattern matching, your collect doesn't work.
I got always stucked with erasure... In every possible implementation I imagined...
Thank you for your precious help