Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] const equivalent for mutable types?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] const equivalent for mutable types?
On Sat, 31 Jul 2004, Christopher A. Gorski wrote:

> In my code I find that I'm passing a lot of mutable values to functions. 
>   Some functions merely read the values.  Others modify the values.  Is 
> there a method in OCaml for reproducing behavior similar in spirit to 
> the const declaration in C?

Yeah.  Don't pass in a reference, pass in what the reference points at.

If you're passing a lot of mutable arguments around, I'd start rethinking 
your design.  Mutable arguments should be few and far between.

Two functional design patterns you might not be aware of, that should help 
eliminate a lot of mutable arguments:

1) Use tuples to return multiple values.  One of the most common 
reasons in C/C++/Java programming to using mutable arguments is to return 
multiple values from a function.  Say you have a function that takes an 
integer and a string, and returns a boolean and a float.  In C/C++, you 
might have a header something like:
    bool foo(int arg1, char * arg2, double * res2_p);
res2_p is the pointer to the location to store the second result, the 
float.  In Ocaml, the function should have the type:
    val foo: int -> string -> bool * float
The advantage of doing this is in Ocaml is that the caller can drop the 
return values into whatever variables they want, like:
    let succeeded, fval = foo(3, "bar") in
    ...

It also makes what is an input to the function and what is an output of 
the function clear.

2) Return the updated data structure.  Another common reason to use 
mutable arguments is to pass in a data structure that the routine then 
modifies.  Instead of having the data structure be modified, have the 
routine return the replacement data structure.  Say you have a StringMap 
module, defined like:
module StringMap = Map.Make(String);;

You want to write a routine that adds the key "foo" and the value 3 to an 
int StringMap.t- thus modifying the data structure.  The function should 
have the type:
val add_foo: int StringMap.t -> int StringMap.t

And might be implemented like:
let add_foo smap = StringMap.add "foo" 3 smap;;

The big advantage here is that the caller gets to decide which version of 
versions of the data structure they continue to use- the old, unmodified 
version, or the new, modified version, or both.


-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners