Knut Arne Vedaa
Knut Arne Vedaa

Reputation: 15742

Exchanging type parameters with abstract types

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?

Upvotes: 4

Views: 234

Answers (1)

Philippe
Philippe

Reputation: 9752

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

val group : G = g

makes it go through.

Upvotes: 3

Related Questions