Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 在不参考原始版本的情况下,为更高种类的类型召唤辅助设备_Scala_Generics_Typeclass_Type Level Computation - Fatal编程技术网

Scala 在不参考原始版本的情况下,为更高种类的类型召唤辅助设备

Scala 在不参考原始版本的情况下,为更高种类的类型召唤辅助设备,scala,generics,typeclass,type-level-computation,Scala,Generics,Typeclass,Type Level Computation,我正在尝试将Aux模式与更高种类的类型一起使用,直到之后才需要指定更高种类类型的参数。这类似于所描述的SO问题,但有一个显著的区别,我将从另一个方向,即从隐式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 ca

我正在尝试将Aux模式与更高种类的类型一起使用,直到之后才需要指定更高种类类型的参数。这类似于所描述的SO问题,但有一个显著的区别,我将从另一个方向,即从隐式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[_]
左右部分中的
是不相关的。这和

type Paramd[A] = Ret[_]

type Paramd[A] = Ret[B] forSome { type B }
因此,
Aux[Prefix,p.Paramd]
与您的定义相当于
Converter[Prefix]{type Paramd[A]=p.Paramd[\u]}
,并且
p
没有此类型,因为
p.Paramd[A]
不是
p.Paramd[\u]