鉴于以下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
上模式匹配?