为所有密封类型的子类型提供隐含

时间:2016-03-11 15:17:52

标签: scala scalaz shapeless scala-macros scala-macro-paradise

在我的应用程序中,我有多个案例类和对象,它们是密封特征层次结构的一部分。我在Akka中将它们用作消息。

在通过websocket发送之前,需要将这些类转换为用户友好的形式。

以前我使用大模式匹配将它们转换为单个位置,但随着类型数量的增加,我想使用隐式转换:

object Types {
  sealed trait Type
  case object SubType1 extends Type
  case object SubType2 extends Type
  case object SubType3 extends Type

  trait Converter[T] {
    def convert(t: T): Int
  }

}

object Implicits {
  import Types._
  implicit object Type1Coverter extends Converter[SubType1.type] {
    override def convert(t: SubType1.type): Int = 1
  }
  implicit object Type2Coverter extends Converter[SubType2.type] {
    override def convert(t: SubType2.type): Int = 2
  }
  implicit object Type3Coverter extends Converter[SubType3.type] {
    override def convert(t: SubType3.type): Int = 3
  }
}

object Conversion {
  import Types._
  def convert[T: Converter](t: T): Int = {
    implicitly[Converter[T]].convert(t)
  }

  def convert2[T <: Type](t: T)(implicit ev1: Converter[SubType1.type], ev2: Converter[SubType2.type], ev3: Converter[SubType3.type]): Int = {
    t match {
      case t1@SubType1 =>
        implicitly[Converter[SubType1.type]].convert(t1)
      case t2@SubType2 =>
        implicitly[Converter[SubType2.type]].convert(t2)
      case t3@SubType3 =>
        implicitly[Converter[SubType3.type]].convert(t3)
    }
  }
}

我想用它们如下:

import Types._
import Conversion._
import Implicits._

val t1 = SubType1
val x1: Int = convert(t1)

val t: Type = SubType2 // T is of type Type
//Is it possible to handle that?
//val x: Int = convert(t)

val y: Int = convert2(t)

我很想知道是否有任何&#34;魔法&#34;在不编写宏的情况下自动生成convert2之类的方法。也许已经有一个像这样提供宏的库?

1 个答案:

答案 0 :(得分:0)

由于在编译时没有关于t类型的信息,因此必须在运行时工作。

如果您将转换器放在sealed family中,则可以使用this question中解释的技术执行类似下面的操作:

import shapeless._

trait AllSingletons[A, C <: Coproduct] {
  def values: List[A]
}

object AllSingletons {
  implicit def cnilSingletons[A]: AllSingletons[A, CNil] =
    new AllSingletons[A, CNil] {
      def values = Nil
    }

  implicit def coproductSingletons[A, H <: A, T <: Coproduct](implicit
                                                              tsc: AllSingletons[A, T],
                                                              witness: Witness.Aux[H]): AllSingletons[A, H :+: T] =
    new AllSingletons[A, H :+: T] {
      def values = witness.value :: tsc.values
    }
}

trait EnumerableAdt[A] {
  def values: Set[A]
}

object EnumerableAdt {
  implicit def fromAllSingletons[A, C <: Coproduct](implicit
                                                    gen: Generic.Aux[A, C],
                                                    singletons: AllSingletons[A, C]): EnumerableAdt[A] =
    new EnumerableAdt[A] {
      def values = singletons.values.toSet
    }
}

object Types {
  sealed trait Type
  case object SubType1 extends Type
  case object SubType2 extends Type
  case object SubType3 extends Type

  sealed abstract class Converter[T <: Type: ClassTag] {
    def convert(t: T): Int
    def convertibleObjectClass = implicitly[ClassTag[T]].runtimeClass
  }

  object Implicits {
    implicit object Type1Converter extends Converter[SubType1.type] {
      override def convert(t: SubType1.type): Int = 1
    }
    implicit object Type2Converter extends Converter[SubType2.type] {
      override def convert(t: SubType2.type): Int = 2
    }
    // let's pretend you FORGOT to add Type3Converter
    //    implicit object Type3Converter extends Converter[SubType3.type] {
    //      override def convert(t: SubType3.type): Int = 3
    //    }
  }
}


object Conversion {
  import Types._
  val AllConverters: Map[Class[_], Converter[_ <: Type]] = implicitly[EnumerableAdt[Converter[_ <: Type]]].values
    .map(c => c.convertibleObjectClass -> c).toMap

  // You're sure you have all the converters here but you can't be sure you remembered to add one per subtype... you have to test it
  // you are sure this cast doesn't fail anyway because of how you created the map
  def findConverter[T <: Type](t: T) = AllConverters.get(t.getClass).asInstanceOf[Option[Converter[T]]]

  def convert2[T <: Type](t: T): Option[Int] = findConverter(t).map(_.convert(t))
}

object Test extends App {
  import Types._
  import Conversion._

  val t: Type = SubType2
  val t2: Type = SubType3

  // works, because a Converter[Subtype2.type] exists
  val a: Option[Int] = convert2(t)
  // returns None, because a Converter[Subtype3.type] doesn't exist
  val b: Option[Int] = convert2(t2)
}