Summon Aux для старшего типа без ссылки на оригинал - PullRequest
2 голосов
/ 29 марта 2019

Я пытаюсь использовать шаблон Aux с типом с более высоким родом и не должен указывать параметр с типом с более высоким родом до тех пор, пока он не будет выполнен. Это похоже на вопрос SO, описанный здесь , но с одним существенным отличием, я иду наоборот, то есть от неявного определения обратно к вспомогательному.

// 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

Что я делаю не так?

1 Ответ

4 голосов
/ 29 марта 2019

Что вы, вероятно, хотите, это

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[_] }, а p не имеет этот тип, потому что p.Paramd[A] не p.Paramd[_].

...