I have a function which takes a GADT as an argument but I’d like it to have a default value. It’s useful to me because I could add functionality to my API without breaking backwards compatibility and I could make the most common case (by far) less verbose.
type _ stringOrInt =
| String: string stringOrInt
| Int: int stringOrInt
let stringOrIntStyle :
'a . string -> ?valueUnit:'a stringOrInt -> 'a -> style =
fun (type a) key ?valueUnit:((valueUnit : a stringOrInt)=String) value ->
match valueUnit with
| String -> StringStyle (key, value)
| Int -> IntStyle (key, value)
It currently doesn’t compile. If I cast
String, the default value, with
Obj.magic it does compile but the code becomes unsafe because the type of
value is not bound by the GADT.
My hopes are low but is it somehow possible to have a default value here?
PS; sorry, that I didn’t use snake_case here but it’s from my BuckleScript/Reason project