Reputation: 2862
I learned that functions accepting arbitrary types could be created, like these:
let f x = x;;
let f x = ();;
let f (x : 'a) = ();;
But I could not find a way to utilize type information inside function, like this:
let print_is = function
| (x : int) -> print_int x
| (s : string) -> print_string s
| _ -> print_string "***";;
Is it really impossible at all, and if so - what is the underlying idea of such restriction? Or I just failed to google properly?
Upvotes: 1
Views: 3326
Reputation: 1739
OCaml does not keep type information at run-time: in order to write the program you suggest, one would need to match on the type of x
, thus one would need a value that represents the type of x
in order to pattern-match on it.
I see two ways to further reply to your question:
Upvotes: 3