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]) =

val mc = new MyClass
val foo = Foo(mc)

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

Need Your Help

Is it possible to chain from one terminal to another via SSH in one series of commands in linux?

linux ssh rsa alias chaining

I am attempting to set up an alias to get from my local box into our main server and then subsequently into an internal box. I'm setting up RSA keys to make this fast, but it would be really nice ...

how could I make a button redirect to another page in play

java scala playframework

I make a simple app in play but I faced a problem when I press the button It didn't respond what could I do?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.