Coproduct上的模式匹配?

时间:2016-12-22 03:10:07

标签: scala shapeless

鉴于以下Shapeless Guide

import shapeless.{Coproduct, :+:, CNil, Inl, Inr}

case class Red()
case class Amber()
case class Green()
type Light = Red :+: Amber :+: Green :+: CNil

我尝试在Light上模式匹配:

scala> def f(l: Light) = l match {
     |   case Red(_) => 42
     |   case _      => 66
     | }
<console>:21: error: constructor cannot be instantiated to expected type;
 found   : Red
 required: shapeless.:+:[Red,shapeless.:+:[Amber,shapeless.:+:[Green,shapeless.CNil]]]
         case Red(_) => 42
              ^

但是,那失败了。如何在Light上模式匹配?

0 个答案:

没有答案