more on type parameters for scala, trying to get a consistent reference to a type

OK, so, I've asked about this before. Ideally I'm looking for a general answer that will help me understand how to specify types consistently, but in lieu of that, I'll settle for how to solve specific problems. So far each solution seems to bring 3 more issues, I'm trying to avoid putting an entire application here but my goal is to find a way to refer to the type of a recursively parameterized trait type from anywhere, in a useful way, in a non-trivial program, where values of that trait type can be used interchangeably.

So, here's more sample code:

//trait file, shouldn't need to know about implementing class.
trait MyTrait[T <: MyTrait[T]] { self:T =>
  val listOfT: List[T]
  def getFirst:T
  def getOne:T = if( !listOfT.isEmpty ) getFirst else self

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 = t.getFirst

  def testMethod1
      [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
              //error! type mismatch.  found:T, required: ?U[?T]

  def testMethod2
       [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
        //error! type mismatch.  
        // found: something.i.type (with underlying type this.MyTrait[T]
        //required: T

  def testMethod3
       [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
        //error: type arguments [U[T]] do not conform to class 
        //Foo's type parameter bounds [A <: this.MyTrait[A]]

  // this works! ...but aren't something.i.getOne and something.i the same type?
  // why does testMethod2 fail if this works ?
  // what if I want to have a method that might return something.i and might return
  // soemthing.i.getOne?  What would the interface for that look like?
  def testMethod4
       [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]

  def testMethod5
       [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
        //error: type mismatch;
        //found: something.i.type (with underlying type this.MyTrait[U[T]]
        // required: U[T]


//class file, shouldn't need to have MyTrait anywhere except 'extends' line.
//should be a usefull class on its own without adding the trait.
class MyClass extends MyTrait[MyClass] {
  //the point of using the parameterized type is to be able to return of 
  //List[MyClass] here instead of List[MyTrait] without having to override
  // some 'type' attribute in anything that uses the trait.
  override val listOfT: List[MyClass] = List[MyClass](this)
  override def getFirst: MyClass = listOfT.head

//some client code:
val mc = new MyClass
val foo = Foo(mc)

I figured out how to use the type parameter: [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]] from an answer to this question: recursive type parameters in case class fields

and I'm basically asking the same thing again, but taking the problem a little further. You can see here that something.i basically has the same type as something.i.getOne, but those types can't be used interchangeably, and so the object can't be consistently used as a parameter to different functions here. How can I make this code work in a way that something.i and something.i.getOne (really probably even the same object) have the same type as recognized by the compiler and type system?

The meat of this particular question is in testMethod4 in the sample code.


The pattern MyTrait[A <: MyTrait[A]] means you will want to work straight with A, and not anymore with MyTrait, as A extends this trait and guarantees to return instances of itself throughout the methods.

Therefore, the mistake is the definition of Foo, it should be simply:

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

With the given correction of Foo, your MyClass compiles and the 'client code', too.

Furthermore, nesting the type like U[X <: MyTrait[X]] <: MyTrait[X]] does not make any sense. Eventually you will have one representation type. Method arguments are in contravariant position, so it totally suffices to have arguments of type T <: MyTrait[ T ] and you can stick in any representation type, no matter how specific. In other words, Foo[U[T]] doesn't have any advantage over Foo[T] but makes things unnecessarily complicated. In all your test methods you can basically remove the U type parameters.

object MyTrait {
  def doSomething[T <: MyTrait[T]](t: T): T = t.getFirst

  def testMethod1[T <: MyTrait[T]](something: Foo[T]): T =

  def testMethod2[T <: MyTrait[T]](something: Foo[T]): T = something.i    
  def testMethod3[T <: MyTrait[T]](something: Foo[T]): T = something.i.getOne
  def testMethod4[T <: MyTrait[T]](something: Foo[T]): T = something.i.getOne
  def testMethod5[T <: MyTrait[T]](something: Foo[T]): T = something.i

Need Your Help

occassional Unknown provider: $templateRequestProvider <- $templateRequest <- $route <- ngViewDirective error

javascript angularjs angular-ui-router bower

I'm working on an angular app, and it works completely fine on computer#1, and when I've cloned the git repo on computer#2 and performed my npm install, bower install, and grunt bower-install, things

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.