On Thu, Oct 15, 2009 at 03:32:30PM +0200,
rixed@... wrote:
> Suppose one have defined the type :
>
> type t = foo:int -> bar:float -> int
>
> Now one want to define many functions of type t with the addition of
> some other optional arguments :
>
> val f1 : ?arg:int -> foo:int -> bar:float -> int
> val f2 : ?arg1:float -> ?arg2:int -> foo:int -> bar:float -> int
> val etc...
Remember in a functional language, functions are first class,
so just return one. The types above are equivalent to:
val f1 : ?arg:int -> t
val f2 : ?arg1:float -> t
How exactly you define them depends rather a lot on what exactly you
want them to do. Here is an plausible example that I came up with:
let print_pair i f = printf "%d, %g\n" i f (* This is the 't' function *)
let prefix_str s = printf "PREFIX<%s> " s; print_pair (* 'f1' *)
let prefix_float f = printf "PREFIX<%g> " f; print_pair (* 'f2' *)
The toplevel gives these the following types:
val print_pair : int -> float -> unit = <fun>
val prefix_str : string -> int -> float -> unit = <fun>
val prefix_float : float -> int -> float -> unit = <fun>
And you can use them like this:
# prefix_float 5.0 1 42.0 ;;
PREFIX<5> 1, 42
# prefix_str "hello" 4 99.0 ;;
PREFIX<hello> 4, 99
Rich.
--
Richard Jones
Red Hat