Given the following type definition:
type 'a range = Full | Range of ('a * 'a);;
How do I convert values of this type to strings?
I currently have:
let string_of_range r = match r with
| Full -> "Full"
| Range(a,b) -> "Range("^(string_of_int a)^","^(string_of_int b)^")";;
But of course, this says that a and b are of type int. They could also be floats or chars (see my previous question about constraining the type)
Change your function to accept a string converter and use it in your implementation. Then when you call it, pass in an appropriate converter and range. Something like:
It will have the type:
string_of_range : ('a -> string) -> 'a range -> stringExample call:
Ordered this way, you could easily make more specialized converters.
It will have the type:
string_of_int_range : int range -> string