D2.0 const this methods

Walter Bright newshound1 at digitalmars.com
Wed Jun 20 15:40:22 PDT 2007


Georg Wrede wrote:
> Walter Bright wrote:
>> Bill Baxter wrote:
>>
>>>  From the const page docs
>>> class Foo {
>>>    invariant int foo() {
>>>       ...
>>>    }
>>> }
>>>
>>> makes 'this' and the return value invariant.
>>
>> No. invariant as a storage class on a member function only makes the 
>> member function invariant, it does not affect the return type.
> 
> Am I the only one who thinks it is hard to remember that a word (here 
> 'invariant') somewhere applies on this and otherwise on that?
> 
> Should we have the keywords applying to the function be written after 
> the function signature, and those applying to the return type before it?
> 
> (I'd really hate D to become another language where one has to remember 
> a truckload of stuff by heart.)

Think of it this way - const as a storage class applies to the top level 
of the type being declared, and everything that type refers to. For a 
member function declaration, then, the const would apply to the member 
function, as that is the top level.

Having const work this way also allows one to group the const member 
function declarations like:

	class Foo
	{
	    const
	    {
		int foo();
		int bar();
	    }
	}

I think it'll be easier to recognize const member functions, because I 
personally find it hard to find the trailing const in a complex declaration.



More information about the Digitalmars-d mailing list