4

To avoid having to repeat type parameter information when subclassing parameterized classes, I'm trying to rewrite some code to use abstract types instead.

What I'd like to express is similar to:

class Group[A]

abstract class Thing[A, G <: Group[A]] {
  val group: G
}

class SomeGroup[A] extends Group[A] { g =>    
  object SomeThing extends Thing[A, SomeGroup[A]] {
    val group = g
  }
}

Using abstract types, my best attempt so far is:

class Group {
  type A
}

abstract class Thing { t =>
  type A
  type G <: Group { type A = t.A }
  val group: G
}

class SomeGroup extends Group { g =>
  object SomeThing extends Thing {
    type A = g.A
    type G = SomeGroup { type A = g.A }
    val group = g
  }
}

However, I'm getting a compiler error on the last line saying "value group has incompatible type".

How can I write the first example using abstract types?

Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59

1 Answers1

3

You need to give the type checker/inferrer a little help:

val group : G = g

makes it go through.

Philippe
  • 9,582
  • 4
  • 39
  • 59
  • That was simpler than I thought. :) Any good reasons why this is necessary here? – Knut Arne Vedaa Dec 23 '11 at 19:10
  • 1
    I would imagine that without any help, the type-checker sees the type of `g` to be just `SomeGroup`, and unless you use a type annotation, that's the type that gets assigned to `group`. Don't take my word for it, though. – Philippe Dec 23 '11 at 19:14