more on type parameters for scala, trying to get a

2019-09-06 14:36发布

问题:

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]]
      (something:Foo[T]):T=
              //error! type mismatch.  found:T, required: ?U[?T]
              doSomething(something.i.getOne)

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

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


  // 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]]
      (something:Foo[T]):T=
        something.i.getOne

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


}

//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)
MyTrait.doSomething(foo.i)
//MyTrait.testMethod1(foo)

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.

回答1:

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 =
    doSomething(something.i.getOne)

  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
}