Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2004-07-31 (14:03)
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 

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: