使用Shapeless来抽象arity

时间:2014-08-09 00:25:21

标签: scala shapeless

说我有:

case class X[F, A](f: F => F, a: A)

然后建立我们:

case class X2[F,A,B](x1: X[F,A], x2: X[F,B]) {
  type I   = (A,B)
  def i: I = (x1.a, x2.a)
  def f    = x2.f compose x1.f
}

case class X3[F,A,B,C](x1: X[F,A], x2: X[F,B], x3: X[F,C]) {
  type I   = (A,B,C)
  def i: I = (x1.a, x2.a, x3.a)
  def f    = x3.f compose x2.f compose x1.f
}

case class X4[F,A,B,C,D](x1: X[F,A], x2: X[F,B], x3: X[F,C], x4: X[F,D]) {
  type I   = (A,B,C,D)
  def i: I = (x1.a, x2.a, x3.a, x4.a)
  def f    = x4.f compose x3.f compose x2.f compose x1.f
}

case class X5[F,A,B,C,D,E] // and so on

Shapeless如何帮助避免上述重复,手动编码每个arity?

注意:结果中的类型成员I是解决方案的必要部分;它并不只是为了澄清i的返回类型。

此探索的下一步是扩展解决方案以支持类似

的内容
case class Y[F,G,A,B]

case class Y2[F,G,A1,B1,A2,B2]      (y1: Y[F,G,A1,B1], y2: Y[F,G,A2,B2])
case class Y3[F,G,A1,B1,A2,B2,A3,B3](y1: Y[F,G,A1,B1], y2: Y[F,G,A2,B2], y3: Y[F,G,A3,B3])

0 个答案:

没有答案