Browse thread
How to cleanly encode "quasi-constants"?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | David.Mentre@i... |
| Subject: | How to cleanly encode "quasi-constants"? |
Hello dear camlists, I'm still learning caml and functionnal programming, so this is maybe a newbie question. Please don't be too rude. In my program, I have variables that can only be modified once (potentially at initialization time after parsing command line and done some computation) and then are never modified for a very long time (rest of program execution). I call them "quasi-constants", they have a write-once semantic. They are used throught the program so it is very painfull to propagate them along function calls (i.e. these variables need a global scope, at least from a programmer point of view). Right now, I'm using constants (let var = ...) or mutable references (let var := ...) to encode such things. However I'm not satisfied. With constants, I obviously can only modify them at compile time. With mutables, I can modify them but potentially at any time, meaning potential bugs. So my question is: What is the Right Way(tm) to encode such a thing in an ML like language and more specifically in OCaml? I was thinking of using a functor (return a module with functions referencing these quasi-constants and quasi-constants values given at module creation). Is it right? It is also possible to use functions, however is seems impracticable for a real program (this approach creates a function many pages long): # let create_manipulator v = let f1 () = v and f2 x = v+x in (f1, f2);; val create_manipulator : int -> (unit -> int) * (int -> int) = <fun> # let (g1, g2) = create_manipulator 3;; val g1 : unit -> int = <fun> val g2 : int -> int = <fun> # g1 ();; - : int = 3 Two side questions: - is there such a problem tackled in a big program ? (ocaml compiler?) - it seems to me that Arg.Set and Arg.Clear types have this semantic (an option is only set at parse time). Best regards, david -- David.Mentre@irisa.fr -- http://www.irisa.fr/prive/dmentre/ Opinions expressed here are only mine.