Embarrassed to ask this question because it seems so trivial but genuinely curious...

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jan 27 18:30:52 UTC 2022


On Thu, Jan 27, 2022 at 05:42:09PM +0000, WhatMeWorry via Digitalmars-d-learn wrote:
> While studying Ali's book at chapter "Constructor and Other Special
> Functions" and the below code snippet:
[...]
>         // Original
>         //this(int i) const { writeln("a const object"); }
>         //this(int i) immutable { writeln("an immutable object"); }
>         //this(int i) shared { writeln("a shared object"); }
> 
>         const this(int i) { writeln("a const object"); }
>         immutable this(int i) { writeln("an immutable object"); }
>         shared this(int i) { writeln("a shared object"); }
[...]
> Assuming I can speak in correct programmer-ese: I was wondering why
> the qualifiers were placed after the function parameter list (int i).
> Just for fun, I moved to type qualifiers before the function
> definitions "this" (like a return type?) and the output was  exactly
> identical.  So I guess my question is, is this just a matter of
> esthetics or is some more nuanced goal at work here?

In method declarations, modifiers like const/immutable/shared play two
distinct roles:

1) Qualifying the return type of the method;
2) Qualifying the implicit `this` parameter of the method.

Historically, D has been rather lax about where qualifiers in the sense
of (2) can go, so that's why:

	const this()

is the same as

	this() const

They both mean that the implicit `this` parameter (not to be confused
with `this` as the name of the ctor) is const.

Personally, though, I prefer the 2nd form, because the first form could
potentially be ambiguous in non-ctor member functions:

	struct S {
		const int method();
	}

Does the const apply to `int` or to the implicit `this` parameter? It's
not obvious. Better to write it this way:

	int method() const;	// const applied to `this`
	const(int) method();	// const applied to return type

N.B. the parentheses in `const(int)` -- while D is lax in allowing you
to write `const int`, that leads to the ambiguous situation above. My
personal recommendation is to always parenthesize qualified types so
that they are never ambiguous.


T

-- 
Mediocrity has been pushed to extremes.


More information about the Digitalmars-d-learn mailing list