Pattern Match on Coproduct?

67 views Asked by At

Given the following from the Shapeless Guide:

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

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

I tried to pattern match on 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
              ^

But, that failed. How can I pattern match on Light?

0

There are 0 answers