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

extending classes with variable parameterized types

3 replies
Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Hi all,
I can do this:
abstract class Foo1(strings: String*)
case class Bar1(strings: String*) extends Foo1(strings: _*)

I can also do this:
abstract class Foo2(stringLists: List[String]*)
case class Bar2(stringLists: List[String]*) extends Foo2(stringLists: _*)

But how do I extend the following class?
abstract class Foo3(stringList: List[_]*)

The following does not compile:
case class Bar3(stringList: List[_]*) extends Foo3(lists: _*)

The compiler complains because it can't determine the type. However, it looks to me like the abstract clas Foo3 cannot be extended ever. Unless, of course, there is some other syntax that I am unaware about that can be used in this situation.

Regards,
Ishaaq
ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.
Re: extending classes with variable parameterized types

Ishaaq Chandy wrote:
> But how do I extend the following class?
> abstract class Foo3(stringList: List[_]*)
>
> The following does not compile:
> case class Bar3(stringList: List[_]*) extends Foo3(lists: _*)
>
> The compiler complains because it can't determine the type.

error: no type parameters for method
sameElements: (Iterable[B])Boolean exist so that it can be applied to
arguments (List[_]*)

Andreas Hofstadler
Joined: 2009-02-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: extending classes with variable parameterized types

Eric Willigers schrieb:
> Ishaaq Chandy wrote:
>> But how do I extend the following class?
>> abstract class Foo3(stringList: List[_]*)
>>
>> The following does not compile:
>> case class Bar3(stringList: List[_]*) extends Foo3(lists: _*)
>>
>> The compiler complains because it can't determine the type.
>
> error: no type parameters for method
> sameElements: (Iterable[B])Boolean exist so that it can be applied to
> arguments (List[_]*)
> --- because ---
> undetermined type
> case class Bar3(stringList2: List[_]*) extends Foo3(stringList2: _*)
> ^
>
>
>
> Curious. The following works
>
> class Bar3(val stringList: List[_]*) extends Foo3(stringList: _*)
>
> object Bar3 {
> def apply(stringList: List[_]*) = new Bar3(stringList: _*)
> def unapply(b: Bar3): Seq[List[_]] = b.stringList
> }
>
Hi,

it works, if you make the existential type explizit:

class Bar3(val stringList: List[T forSome {type T}]) extends
Foo3(stringList: _*)

The only thing is, that stringList has the type
T forSome {type T <: Any}
and not
T forSome {type T <: String}
the name of the parameter is misleading.

greets

Andi

Ishaaq Chandy
Joined: 2009-02-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: extending classes with variable parameterized types
thanks for that Andreas, yes the parameter name was a copy-paste error on my part.

Ishaaq

2009/2/27 Andreas Hofstadler <hofstadler.andi@gmx.at>
Eric Willigers schrieb:
Ishaaq Chandy wrote:
But how do I extend the following class?
abstract class Foo3(stringList: List[_]*)

The following does not compile:
case class Bar3(stringList: List[_]*) extends Foo3(lists: _*)

The compiler complains because it can't determine the type.

error: no type parameters for method
sameElements: (Iterable[B])Boolean exist so that it can be applied to arguments (List[_]*)

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