Author |
Message |
wtd
|
Posted: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
sw2wolf
|
Posted: 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 |
|
|
|
|
|
wtd
|
Posted: 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. |
|
|
|
|
|
Aange10
|
Posted: 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 |
|
|
|
|
|
Tony
|
|
|
|
|
wtd
|
Posted: 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);; |
|
|
|
|
|
|
|