What is the purpose of the emptyCoproduct and coproduct methods of the TypeClass trait in Shapeless

前端 未结 2 2068
失恋的感觉
失恋的感觉 2021-02-04 13:27

It is not fully clear to me what is the purpose of the emptyCoProduct and coproduct methods of the TypeClass trait in Shapeless.

W

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-04 13:48

    As of shapeless 2.3.2, the above example doesn't seem to compile. here's the updated one for future reference:

    import shapeless._
    import shapeless.test._
    
    trait Weight[A] { def apply(a: A): Int }
    
    object Weight {
      def apply[A](f: A => Int) = new Weight[A] { def apply(a: A) = f(a) }
    }
    
    case class Foo(i: Int, s: String)
    
    sealed trait Root
    case class Bar(i: Int) extends Root
    case class Baz(s: String) extends Root
    
    object Base {
      implicit val stringWeight: Weight[String] = Weight(_.size)
      implicit def intWeight: Weight[Int] = Weight(identity)
    }
    
    object ProductTC {
      object WeightHelper extends ProductTypeClassCompanion[Weight] {
        object typeClass extends ProductTypeClass[Weight] {
          def emptyProduct: Weight[HNil] = Weight(_ => 0)
          def product[H, T <: HList](hw: Weight[H], tw: Weight[T]): Weight[H :: T] =
            Weight { case (h :: t) => hw(h) + tw(t) }
          def project[F, G](w: => Weight[G], to: F => G,from: G => F): Weight[F] =
            Weight(f => w(to(f)))
        }
      }
    
      import Base._
      import WeightHelper._
    
      implicitly[Weight[Foo]]
      implicitly[Weight[Bar]]
      implicitly[Weight[Baz]]
    
      illTyped("implicitly[Weight[Root]]")
    }
    
    object TC {
      object WeightTypeClass extends TypeClassCompanion[Weight] {
        object typeClass extends TypeClass[Weight] {
          def emptyProduct: Weight[HNil] = Weight(_ => 0)
          def product[H, T <: HList](hw: Weight[H], tw: Weight[T]): Weight[H :: T] =
            Weight { case (h :: t) => hw(h) + tw(t) }
          def project[F, G](w: => Weight[G], to: F => G, from: G => F): Weight[F] =
            Weight(f => w(to(f)))
          def emptyCoproduct: Weight[CNil] = Weight(_ => 0)
          def coproduct[L, R <: Coproduct]
            (lw: => Weight[L], rw: => Weight[R]): Weight[L :+: R] = Weight {
            case Inl(h) => lw(h)
            case Inr(t) => rw(t)
          }
        }
      }
    
      import Base._
      import WeightTypeClass._
    
      implicitly[Weight[Root]]
    }
    

提交回复
热议问题