- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
extending classes with variable parameterized types
Thu, 2009-02-26, 01:26
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
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
Thu, 2009-02-26, 21:57
#2
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
Thu, 2009-02-26, 22:47
#3
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>
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[_]*)
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[_]*)