
-----------------------------------
wtd
Sat Jan 10, 2009 4:47 am

O'Caml TYS: many functions... one default
-----------------------------------
Let's say I want to have a bunch of functions that take a default value.

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.  ;-)

-----------------------------------
sw2wolf
Fri Nov 18, 2011 10:42 pm

Re: O'Caml TYS: many functions... one default
-----------------------------------
let defVal = 42

let foo ?(bar=defVal) () = bar *3

-----------------------------------
wtd
Sat Nov 26, 2011 10:49 pm

RE:O\'Caml TYS: many functions... one default
-----------------------------------
Good on you for trying, but I disqualified that approach in the original post.

-----------------------------------
Aange10
Sat Nov 26, 2011 11:42 pm

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

-----------------------------------
Tony
Sat Nov 26, 2011 11:59 pm

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;;
[/code]
:lol:

-----------------------------------
wtd
Mon Nov 28, 2011 1:03 am

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);;[/code]
