Computer Science Canada

O'Caml TYS: many functions... one default

Author:  wtd [ Sat Jan 10, 2009 4:47 am ]
Post subject:  O'Caml TYS: many functions... one default

Let's say I want to have a bunch of functions that take a default value.

code:
let foo ?(bar = 42) () = bar * 3;;

let baz ?(wooble = 42) () = wooble / 2;;


Now, what if I want to change that default? I'd have to edit the code in several places.

Figure out a way that I can change that default uniformly across both functions, without having to modify the functions themselves. Storing the default as a separate let binding doesn't count. Wink

Author:  sw2wolf [ Fri Nov 18, 2011 10:42 pm ]
Post subject:  Re: O'Caml TYS: many functions... one default

let defVal = 42

let foo ?(bar=defVal) () = bar *3

Author:  wtd [ Sat Nov 26, 2011 10:49 pm ]
Post subject:  RE:O\'Caml TYS: many functions... one default

Good on you for trying, but I disqualified that approach in the original post.

Author:  Aange10 [ Sat Nov 26, 2011 11:42 pm ]
Post subject:  RE:O\'Caml TYS: many functions... one default

Oh come on wtd, it took two years for somebody to gut up and answer. Cut 'em some slack

Author:  Tony [ Sat Nov 26, 2011 11:59 pm ]
Post subject:  RE:O\'Caml TYS: many functions... one default

can I use an inverse of a function? I.e., something like
code:

let foo ?(bar = 42) () = bar * 3;;

let baz ?(wooble = foo() / 3) () = wooble / 2;;

Laughing

Author:  wtd [ Mon Nov 28, 2011 1:03 am ]
Post subject:  RE:O\'Caml TYS: many functions... one default

code:
module X =
   functor (T : sig default : int end) ->
      struct
         let foo () = T.default / 2
         let baz x = T.default * x
      end;;

let module Y = X(struct let default = 42 end) in
   Printf.printf "%d, %d\n" (Y.foo ()) (Y.baz 3);;


: