DEV Community

Discussion on: Inlined values in BuckleScript

Collapse
 
yawaramin profile image
Yawar Amin

So, there are two possible answers here. The first and direct answer is, you can do something like

let test = color =>
  Js.log(switch (color :> string) {
    | "red" => "It's red!"
    | "black" => "It's black!"
    | _ => "I don't know what it is!"
  });

The main trick in this code snippet is the part (color :> string) which upcasts the color value (of type Color.t) into a string. That's what the private type enables–it lets the type be upcast into its supertype, in this case string.

The second answer here is that, in this example Color.t values are not really meant to be consumed from the Reason side. This Color module is really a helper for JS interop. It's meant to safely send exact string values into JavaScript functions. If you need to pattern-match i.e. consume these values that indicates there's something else going on and maybe a different approach is required. You can kind of tell because of the catch-all pattern _ at the end of the switch–you're switching on a 'lower level' value like a string so you don't have a limited set of cases like a variant.