English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] RFC: get/set vs get/ref
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-07-14 (01:14)
From: John Max Skaller <skaller@m...>
Subject: [Caml-list] RFC: get/set vs get/ref
[posted to caml-list@inria.fr and boost@yahoogroups.com]

I seek your opinion on  get/set vs. get/ref.

Suppose we desire to abstract an immutable data type.
We can do that by providing 'get' methods to access
the data. The canonical examples are

	a) tuples: represented by projection functions
	b) lists: represented by 'head' and 'tail' functions

When the data type is mutable, there are two choices.
The simplest choice is to provide set methods.
For example, for a mutable string, with C++ notation:

	char string::get_char (int pos) const
	void string::set_char (int pos, char ch)

For Algol like languages, we could also provide references:

	char string::get_char (int pos)const
	char &string::ref_char (int pos)

The difference is exemplified by the following techniques
for incrementing a character:

	s.set ((s.get(pos) + 1),pos)  // get/set method
	s.ref(pos).++                 // ref method

Clearly, ref methods are more powerful and more efficient,
but on the other hand they expose the underlying implementation
and prevent hooking changes to the mutable state.

What's the best technique?

John (Max) Skaller, mailto:skaller@maxtal.com.au 
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
New generation programming language Felix  http://felix.sourceforge.net
Literate Programming tool Interscript     
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr