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?


I got this to work:

trait MyTrait[T <: MyTrait[T]] { def getFirst = this } class MyClass extends MyTrait[MyClass] case class Foo[A <: MyTrait[A]](i: MyTrait[A]) object MyTrait { def doSomething[T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]](t: U[T]) = t.getFirst } val mc = new MyClass val foo = Foo(mc) MyTrait.doSomething(foo.i)


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


  • How can I select multiple values from the inList constraint in Grails?
  • Customize NSMenu (like Apple's Dock right click Menu)?
  • Android-Ndk vs Cross-Compile? Both work ,What was the need of Android NDK then ?
  • HSQL + Hibernate Exception: Wrong column type: Found: double, expected: float
  • Declare a C++ class without defining it in the current translation unit
  • PHP & cURL proxy - how to make multi-user cookie jar?
  • Call task's updateProgress
  • Using constants or global variables in 3 tier console application
  • Checking a play current mode makes an error occur
  • Tell Git to stop prompting me for conflicts when none really exist?
  • Cast between interfaces whose interface signatures are same
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • How to Cache Real-time Data?
  • How to use RequestBodyAdvice
  • Jquery UI tool tip close icon
  • Highlight and Bold text in JTextPane
  • Q promise. Difference between .when and .then
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • Display issues when we change from one jquery mobile page to another in firefox
  • JFileChooser in front of fullscreen Swing application
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Incrementing object id automatically JS constructor (static method and variable)
  • Does CUDA 5 support STL or THRUST inside the device code?
  • When should I choose bucket sort over other sorting algorithms?
  • Hazelcast - OperationTimeoutException
  • To display the title for the current loaction in map in iphone
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app