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] 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 and]

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, 
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
New generation programming language Felix
Literate Programming tool Interscript
Bug reports:  FAQ:
To unsubscribe, mail  Archives: