Scala 我可以使用类的类型注释来决定类的方法的结果类型吗?
我有一些有两个继承人的Scala 我可以使用类的类型注释来决定类的方法的结果类型吗?,scala,typeclass,Scala,Typeclass,我有一些有两个继承人的 trait MyTrait[T } 类MyTraitImpl1扩展了MyTrait[MyTraitImpl1] 类MyTraitImpl2扩展了MyTrait[MyTraitImpl2] 这些继承器是互斥的,并且可以从彼此派生,因此还有另一个使用MyTrait键入的类 classtypedclass[T如果您可以修改MyTrait,MyTraitImpl1,MyTraitImpl2您可以添加类型成员OtherType trait MyTrait[T <: My
trait MyTrait[T
}
类MyTraitImpl1扩展了MyTrait[MyTraitImpl1]
类MyTraitImpl2扩展了MyTrait[MyTraitImpl2]
这些继承器是互斥的,并且可以从彼此派生,因此还有另一个使用MyTrait
键入的类
classtypedclass[T如果您可以修改MyTrait
,MyTraitImpl1
,MyTraitImpl2
您可以添加类型成员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
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 = ???
}