Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 O'Caml TYS: many functions... one default
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
sw2wolf




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: 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);;
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: