This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Type parameter, collection and return type

3 replies
Ka Ter
Joined: 2011-10-05,
User offline. Last seen 42 years 45 weeks ago.

Hi,

why is the following code producing the compiler error 'type mismatch;
found : Traversable[Any] required: C'? The type of the collection
doesn't change and therefore it is a C. If I ommit the ':C' of the
method than the return type is inferred to Traversable[Any].

class Test[C <: Traversable[Any] : Manifest](collection: C)
{
def test(): C = collection.filter(e => e == e)
}

(new Test(Set[String]())).test

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Re: Type parameter, collection and return type
Because C might be a supertype of Traversable[Any], but filter will return a Traversable[Any]. Look at the filter signature on scala collections.

Best RegardsEdmondo Porcu

2012/2/9 Ka Ter <ter.ka966@googlemail.com>
Hi,

why is the following code producing the compiler error 'type mismatch;
found   : Traversable[Any]  required: C'? The type of the collection
doesn't change and therefore it is a C. If I ommit the ':C' of the
method than the return type is inferred to Traversable[Any].

       class Test[C <: Traversable[Any] : Manifest](collection: C)
       {
           def test(): C = collection.filter(e => e == e)
       }

       (new Test(Set[String]())).test

--
Best Regards

Ka Ter


H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Type parameter, collection and return type

def worksLikeThis[A, B[A] <: Traversable[A]](c: B[A]) = c.filter(e => true)

Am 09.02.2012 17:55, schrieb Ka Ter:
> Hi,
>
> why is the following code producing the compiler error 'type mismatch;
> found : Traversable[Any] required: C'? The type of the collection
> doesn't change and therefore it is a C. If I ommit the ':C' of the
> method than the return type is inferred to Traversable[Any].
>
> class Test[C <: Traversable[Any] : Manifest](collection: C)
> {
> def test(): C = collection.filter(e => e == e)
> }
>
> (new Test(Set[String]())).test
>

Ka Ter
Joined: 2011-10-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Type parameter, collection and return type

Ok, I tried two variants, but none of them compile:

1)
class Element

def test[C <: Traversable[Element], B, That](traversable: C)(
implicit cbf: CanBuildFrom[C, B, That]): That =
traversable.map(a => a)(cbf)

test(Set[Element]()).contains(new Element)

2)
class Element2

def test2[E <: Element, C[E] <: Traversable[E], B,
That](traversable: C[E])(
implicit cbf: CanBuildFrom[C[E], B, That]): That =
traversable.map(a => a)(cbf)

test2(Set[Element]()).contains(new Element2)

The problem is in both cases the following:

1) type mismatch; found :
scala.collection.generic.CanBuildFrom[C,B,That] required:
scala.collection.generic.CanBuildFrom[Traversable[Element],Element,That]

2) type mismatch; found :
scala.collection.generic.CanBuildFrom[C[E],B,That] required:
scala.collection.generic.CanBuildFrom[Traversable[E],E,That]
NewTestQuery.scala

How can I get the method test to emit a Set thus I can use the contains
Method?

Best Regards

Ka Ter

Am 09.02.2012 18:38, schrieb HamsterofDeath:
> def worksLikeThis[A, B[A] <: Traversable[A]](c: B[A]) = c.filter(e => true)
>
> Am 09.02.2012 17:55, schrieb Ka Ter:
>> Hi,
>>
>> why is the following code producing the compiler error 'type mismatch;
>> found : Traversable[Any] required: C'? The type of the collection
>> doesn't change and therefore it is a C. If I ommit the ':C' of the
>> method than the return type is inferred to Traversable[Any].
>>
>> class Test[C <: Traversable[Any] : Manifest](collection: C)
>> {
>> def test(): C = collection.filter(e => e == e)
>> }
>>
>> (new Test(Set[String]())).test
>>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland