DIP53 and DIP49 (ver2) - New definitions for qualified constructors and postblits

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Dec 18 10:40:22 PST 2013


On Wed, Dec 18, 2013 at 06:33:37PM +0100, Joseph Rushton Wakeling wrote:
> On 18/12/13 18:20, Timon Gehr wrote:
> >Well, 'const' attached to a method is simply the notation used for
> >attaching 'const' to the implicit 'this' parameter.
> 
> Sure, but if you see a method:
> 
>    struct Foo
>    {
>        const int bar() { ... }
>    }
> 
> ... the instinctive interpretation of that is "This is a method that
> returns a const int."  You soon learn your mistake, but once you
> have, the mental association between one form of const and the other
> makes sense, even if you don't appreciate the subtlety of the fact
> that it's referring to the implicit "this" parameter.
[...]

For this reason, it is better to write 'const' on the right side of the
function name than on the left:

	struct Foo
	{
		// Now it's clear(er) that 'const' refers to const(this)
		// rather than const(int).
		int bar() const { ... }
	}

And also to always write `const(int)` rather than the potentially
ambiguous `const int`. It may feel unnecessarily verbose at first, until
you have to distinguish between:

	const(int)[] func();
	const(int[]) func();
	int[] func() const;
	const(int)[] func() const;
	const(int[]) func() const;

then you begin to appreciate the notation. :)


T

-- 
Almost all proofs have bugs, but almost all theorems are true. -- Paul Pedersen


More information about the Digitalmars-d mailing list