我可以使用类的类型注释来决定类的方法的结果类型吗?

问题描述 投票:2回答:1

我有一些有两个继承人。

trait MyTrait[T <: MyTrait[T]] {
  this: T =>
}

class MyTraitImpl1 extends MyTrait[MyTraitImpl1]
class MyTraitImpl2 extends MyTrait[MyTraitImpl2]

这些继承者是互斥的,可以相互派生,因此有另一个用MyTrait键入的类。

class TypedClass[T <: MyTrait](value: T) {

  def anotherValue[O <: MyTrait]: O
}

如果用anotherValue输入MyTraitImpl1,反之亦然,我能做些什么来使方法TypedClass完全返回MyTraitImpl2吗?

scala typeclass
1个回答
4
投票

如果你可以修改MyTraitMyTraitImpl1MyTraitImpl2你可以添加类型成员OtherType

  trait MyTrait[T <: MyTrait[T]] {
    this: T =>
    type OtherType <: MyTrait[_]
  }

  class MyTraitImpl1 extends MyTrait[MyTraitImpl1] {
    override type OtherType = MyTraitImpl2
  }
  class MyTraitImpl2 extends MyTrait[MyTraitImpl2] {
    override type OtherType = MyTraitImpl1
  }


  class TypedClass[T <: MyTrait[T]](value: T) {

    def anotherValue: T#OtherType = ???
  }

//    OR
//  class TypedClass[T <: MyTrait[T]](val value: T) {
//
//    def anotherValue: value.OtherType = ???
//  }

  new TypedClass[MyTraitImpl1](new MyTraitImpl1).anotherValue : MyTraitImpl2
  new TypedClass[MyTraitImpl2](new MyTraitImpl2).anotherValue : MyTraitImpl1
//  new TypedClass[MyTraitImpl1](new MyTraitImpl1).anotherValue : MyTraitImpl1 // doesn't compile
//  new TypedClass[MyTraitImpl2](new MyTraitImpl2).anotherValue : MyTraitImpl2 // doesn't compile

如果你不能修改MyTraitMyTraitImpl1MyTraitImpl2你可以创建类型类OtherType

  trait MyTrait[T <: MyTrait[T]] {
    this: T =>
  }

  class MyTraitImpl1 extends MyTrait[MyTraitImpl1]
  class MyTraitImpl2 extends MyTrait[MyTraitImpl2]

  trait OtherType[T <: MyTrait[T]] {
    type Out <: MyTrait[_]
  }

  object OtherType {
    type Aux[T <: MyTrait[T], Out0 <: MyTrait[_]] = OtherType[T] { type Out = Out0 }
    def instance[T <: MyTrait[T], Out0 <: MyTrait[_]]: Aux[T, Out0] = new OtherType[T] { type Out = Out0 }

    implicit val otherType1: Aux[MyTraitImpl1, MyTraitImpl2] = instance
    implicit val otherType2: Aux[MyTraitImpl2, MyTraitImpl1] = instance
  }

  class TypedClass[T <: MyTrait[T]](value: T) {

    def anotherValue(implicit otherType: OtherType[T]): otherType.Out = ???
  }
© www.soinside.com 2019 - 2024. All rights reserved.