Provide implicits for all subtypes of sealed type

自闭症网瘾萝莉.ら 提交于 2019-12-06 14:58:58
Giovanni Caporaletti

Since you have no info at compile time about t's type, you have to work at runtime.

if you put your Converters in a sealed family, you could do something like the follwing, using a technique explained in 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)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!