Suggestion: object const'ness

Sjoerd van Leent svanleent at gmail.com
Thu May 25 23:34:25 PDT 2006


ChristopheBourez schreef:
> It should be interesting to have a const'ness ala C++, I mean, the ability to
> declare methods that do not modify the state of objects.
> 
> class X 
> { 
> public:
> void ModifyState?();
> void DontModifyState?() const;
> };
> 
> Any comments?
> 
> Christophe
> 
> 

Endless discussion which has followed on this, discussing pros and cons 
about D vs. C++ and so forth.

What about the following:

using another parameter such as:

locked

void foo(in locked Bar bar) {
	bar.setValue(10); // Compiler error
	int i = bar.getValue(); // OK
}

The keyword would modify the behaviour from in, which only is about the 
actual stack value passed to the function, to be completely locking all 
calls (changing the reference, changing the object contents, changing 
the contents within the object contents etc).

It would be possible using the same keyword prepended in front of 
functions themselves:

class Bar {
	int _i;
	void setValue(int i) {_i = i;}

	// Can't modify _i contents nor (if a reference) the
	// contents within.
	locked int getValue() {return _i;}

}

How about this?



More information about the Digitalmars-d mailing list