views:

232

answers:

3

In Scala v 2.7.7

I have a file with

class Something[T] extends Other

object Something extends OtherConstructor[Something]

This throws the error:

class Something takes type parameters
object Something extends OtherConstructor[Something] {

However, I can't do this

object Something[T] extends OtherConstructor[Something[T]]

It throws an error:

error: ';' expected but '[' found.

Is it possible to send type parameters to object? Or should I change and simply use Otherconstructor

+3  A: 

An object has to have a concrete type. The Scala object contruct is not a exception to this rule.

A valid definition is

object Something extends OtherConstructor[Something[T]] { }

where T is some concrete type.

Thomas Jung
+4  A: 

You could use:

object Something extends OtherConstructor[Something[_]]

You will of course be restricted by having an existential type with no upper bound in place instead of a concrete type. This solution may not make sense and you might need one object per concrete type T, for those T's which you care about, e.g.

object StringSomething extends OtherConstructor[Something[String]]

But then this has the (possible) disadvantage that StringSomething is not the companion object of Something.

However, my advice would be don't start messing about designing generic APIs (especially self-referential ones like the above) unless you really, really know what you are doing. It will almost certainly end in tears and there are plenty of CORE Java API's which are terrible because of the way generics have been added (the RowSorter API on JTable being one example)

oxbow_lakes
The APT API is nice too: `List<? extends AnnotationMirror> getAnnotationMirrors()` (http://java.sun.com/javase/6/docs/api/javax/lang/model/element/Element.html#getAnnotationMirrors%28%29) ;-)
Thomas Jung
A: 

Thanks for the answers

object Something extends OtherConstructor[Something[_]]

seems to be compiling (although I have yet to run/test that :-))

@oxbow_lakes, I've followed your advice - of avoiding the type system - till now but I've got to do it!!! I've been studying existential types, type-erasure and all that but its still not in my grasp :-(

SiM