- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
List of Classes of arbitrary type
Tue, 2009-03-03, 21:40
Hi,
How can I create a List of Classes of arbitrary type?
For a List[Class[AnyRef]] the compiler won't allow me to use
List(classOf[String]).
Thanx
Heiko
Tue, 2009-03-03, 21:57
#2
Re: List of Classes of arbitrary type
Heiko Seeberger wrote:
> Hi,
>
> How can I create a List of Classes of arbitrary type?
> For a List[Class[AnyRef]] the compiler won't allow me to use
> List(classOf[String]).
>
> Thanx
> Heiko
In a perfect world, the type parameter of the 'Class' class would have
been declared to be co-variant. E.g.
class Class[+T] ...
However, the world is not perfect and this class was defined in Java,
not Scala. To deal with this, Scala has "existential types". If you
declare your list like in the snippet below it should work:
var classes = List[Class[_ <: Object]]()
classes += classOf[Object]
classes += classOf[String]
Cheers,
Roman
Tue, 2009-03-03, 22:07
#3
Re: List of Classes of arbitrary type
scala> class Foo; class Bar
defined class Foo
defined class Bar
scala> val x : List[Class[_]] = List(classOf[String], classOf[Int], classOf[Foo], classOf[Bar])
x: List[Class[_]] = List(class java.lang.String, int, class Foo, class Bar)
On Tue, Mar 3, 2009 at 12:40 PM, Heiko Seeberger <heiko.reg@seebergers.de> wrote:
defined class Foo
defined class Bar
scala> val x : List[Class[_]] = List(classOf[String], classOf[Int], classOf[Foo], classOf[Bar])
x: List[Class[_]] = List(class java.lang.String, int, class Foo, class Bar)
On Tue, Mar 3, 2009 at 12:40 PM, Heiko Seeberger <heiko.reg@seebergers.de> wrote:
Hi,
How can I create a List of Classes of arbitrary type?
For a List[Class[AnyRef]] the compiler won't allow me to use List(classOf[String]).
Thanx
Heiko
Wed, 2009-03-04, 00:37
#4
Re: List of Classes of arbitrary type
List[Class[+AnyRef]] ?
BR,
John
On Tue, Mar 3, 2009 at 9:44 PM, Heiko Seeberger
wrote:
> Hi,
>
> How can I create a List of Classes of arbitrary type?
> For a List[Class[AnyRef]] the compiler won't allow me to use
> List(classOf[String]).
>
> Thanx
> Heiko
>
Wed, 2009-03-04, 00:47
#5
Re: List of Classes of arbitrary type
Try List[Class[_]]
--j
On Tue, Mar 3, 2009 at 12:44 PM, Heiko Seeberger <heiko.seeberger@googlemail.com> wrote:
--j
On Tue, Mar 3, 2009 at 12:44 PM, Heiko Seeberger <heiko.seeberger@googlemail.com> wrote:
Hi,
How can I create a List of Classes of arbitrary type?
For a List[Class[AnyRef]] the compiler won't allow me to use List(classOf[String]).
Thanx
Heiko
Hi,
How can I create a List of Classes of arbitrary type?
For a List[Class[AnyRef]] the compiler won't allow me to use
List(classOf[String]).
Thanx
Heiko