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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With