Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recursive type parameters in case class fields

OK, so, I get that I can do things like:

trait MyTrait[T <: MyTrait[T]] { self: T =>
  val listOfT: List[T]
  def getFirst: T
  def getOne: T = if (listOfT.length > 0) getFirst else self
}

class MyClass extends MyTrait[MyClass] {
  override val listOfT: List[MyClass] = List[MyClass](this)
  override def getFirst: MyClass = listOfT.head
}

and that if I want MyTrait to have a companion object it looks like:

object MyTrait{
  def doSomething[T <: MyTrait[T]](aninstance:T)= { ... }
}

All that seems ugly and I'd like to see a nicer way, but, right now I'm just trying to figure out, how do I refer to the type from anywhere else? For example:

case class Foo( anInstanceOfMyTrait: MyTrait[what goes here???] )

Or is there a simpler way?

like image 563
nairbv Avatar asked Jul 14 '26 23:07

nairbv


1 Answers

Looking at your comment it seems the actual problem is that in your attempt to parameterize Foo, you referenced MyTrait twice:

case class Foo[A <: MyTrait[A]](i:MyTrait[A])

Try this instead:

case class Foo[A <: MyTrait[A]](i: A)

This mirrors the way MyTrait.doSomething is defined, so that you can pass i to doSomething:

case class Foo[A<:MyTrait[A]](i: A)
val foo = new Foo[MyClass]( new MyClass )
MyTrait.doSomething( foo.i ) // This compiled OK
like image 194
Régis Jean-Gilles Avatar answered Jul 17 '26 16:07

Régis Jean-Gilles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!