我正在尝试将 Aux 模式与更高种类的类型一起使用,并且直到之后才必须指定更高种类类型的参数。这类似于所描述的SO问题here https://stackoverflow.com/questions/52581986/aux-pattern-for-higher-kinded-types但有一个显着的区别,我将采取相反的方式,即从隐式 def 回到 aux。
// The are types that I want to convert to various things
sealed trait ConversionType
trait CaseA extends ConversionType
object CaseA extends CaseA // In this case, convert to an optional
trait CaseB extends ConversionType
object CaseB extends CaseB // In this case, convert to a future etc...
trait Converter[Prefix] {
type Paramd[_]
def create[N](n:N): Paramd[N]
}
// Create the mechanism to convert from the cases, only doing case A for now...
object Converter {
type Aux[Prefix, Ret[_]] = Converter[Prefix] { type Paramd[_] = Ret[_] }
// *** Error happens here! ***
def apply[Prefix](implicit p:Converter[Prefix]): Aux[Prefix, p.Paramd] = p
implicit def makeOptionParamd: Aux[CaseA, Option] =
new Converter[CaseA] {
type Paramd[_] = Option[_]
override def create[N](n:N): Paramd[N] = Option[N](n)
}
}
// This seems to be fine...
val v = Converter.apply[CaseA].create("test")
我在上面提到的行上收到以下编译错误:
Error:(97, 78) type mismatch;
found : p.type (with underlying type Test.this.Converter[Prefix])
required: Test.Converter.Aux[Prefix,p.Paramd]
(which expands to) Test.this.Converter[Prefix]{type Paramd[_] = p.Paramd[_]}
def apply[Prefix](implicit p:Converter[Prefix]): Aux[Prefix, p.Paramd] = p
我究竟做错了什么?
你可能想要的是
object Converter {
type Aux[Prefix, Ret[_]] = Converter[Prefix] { type Paramd[A] = Ret[A] }
// compiles
def apply[Prefix](implicit p:Converter[Prefix]): Aux[Prefix, p.Paramd] = p
implicit def makeOptionParamd: Aux[CaseA, Option] =
new Converter[CaseA] {
type Paramd[A] = Option[A]
override def create[N](n:N): Paramd[N] = Option[N](n)
}
}
当你写的时候
type Paramd[_] = Ret[_]
the _
左右部分是不相关的。它是一样的
type Paramd[A] = Ret[_]
type Paramd[A] = Ret[B] forSome { type B }
So Aux[Prefix, p.Paramd]
你的定义相当于Converter[Prefix] { type Paramd[A] = p.Paramd[_] }
, and p
没有这个类型,因为p.Paramd[A]
is not p.Paramd[_]
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)