如何删除重复的case语句以实现密封特征

时间:2015-04-15 13:23:17

标签: scala shapeless

我经常发现在为每个实现做同样的事情之前我需要提取密封特征的类型:

sealed trait Trait
case class Foo() extends Trait
case class Bar() extends Trait
// ... lots of other implementations

// *must* take a `Trait`, not a `T <: Trait`
def thing(t: Trait): ??? = t match {
  case f: Foo => // something with the instance and specific type
  case b: Bar => // something with the instance and specific type 
  // ... same thing again for other implementations
}

例如

// typically provided by somebody else...
trait Thing[T] { def thingy: String }
implicit def thing[T]: Thing[T] = new Thing[T] { def thingy = "stuff" }

def thing(t: Trait): String = t match {
  case Foo() => implicitly[Thing[Foo]].thingy
  case Bar() => implicitly[Thing[Bar]].thingy
  // ...
}

我希望减少参与此操作的样板。

1 个答案:

答案 0 :(得分:4)

UPDATE :现在我们通过无形的方式使用类型派生。例如https://github.com/fommil/shapeless-for-mortals

事实证明,你可以使用无形的&#39;多态函数和联产品来做到这一点:

  object thing extends Poly1 {
    implicit def action[T <: Trait: Thing] = at[T](
      a => implicitly[Thing[T]].thingy
    )
    // magic that makes it work at the sealed trait level
    def apply(t: Trait): String =
      Generic[Trait].to(t).map(thing).unify
  }

然后可以像

一样使用
println(thing(Foo(): Trait))

我希望通过抽象类更容易编写(让我们暂时忘记将隐式参数传递给action),例如。

abstract class MatchSealed[In, Out] extends Poly1 {
  implicit def poly[T <: In] = at[T](action)

  def action[T <: In](t: T): Out

  import ops.coproduct.Mapper
  def apply[R <: HList](in: In)(
    implicit
      g: Generic.Aux[In, R],
      m: Mapper[this.type, R]
  ): Out = {
    val self: this.type = this
    g.to(in).map(self).unify
  }
}

但是在最后一行丢失Mapper[self.type, g.Repr]时失败了。我不知道哪个隐含缺失,但我怀疑它是self.type。我真的想抓住realisedSelf.type,但我不知道该怎么做。

更新:结果是无法获取Mapper,因为它需要访问已实现的object Unable to map on HList