Programming languages enthusiast. Author of Learn Type Driven Development: https://www.packtpub.com/application-development/learn-type-driven-development
So, there are two possible answers here. The first and direct answer is, you can do something like
lettest=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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
So, there are two possible answers here. The first and direct answer is, you can do something like
The main trick in this code snippet is the part
(color :> string)
which upcasts thecolor
value (of typeColor.t
) into astring
. That's what the private type enables–it lets the type be upcast into its supertype, in this casestring
.The second answer here is that, in this example
Color.t
values are not really meant to be consumed from the Reason side. ThisColor
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 theswitch
–you're switching on a 'lower level' value like a string so you don't have a limited set of cases like a variant.