|
Boost : |
From: John Max Skaller (skaller_at_[hidden])
Date: 2001-07-11 20:49:01
[posted to caml-list_at_[hidden] and boost_at_[hidden]]
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_at_[hidden] 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 http://Interscript.sourceforge.net
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk