D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 9597 - using "this" as a type leads to confusion
Summary: using "this" as a type leads to confusion
Status: RESOLVED DUPLICATE of issue 18228
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-02-26 11:23 UTC by Ivan Kazmenko
Modified: 2018-05-12 13:22 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Ivan Kazmenko 2013-02-26 11:23:52 UTC
Using "this" as a type in method parameter declarations is not universal because of the postblit constructor signature.  It also creates confusion when the method signature resembles the postblit constructor.

Here are a few examples of what can and cannot be declared (DMD 2.062):
-----
struct S
{
	int x = 1;
	int y = 0;

	this (this) // postblit constructor called like this: "S b = a;"
	{
		x = 10;
		y = 1;
	}

	this (ref this) // constructor called as: "S c = S (a);"
	{
		x = 100;
	}

	this (S q) // compiles, example call: "S d = S (S (a));"
	{
		x = 1000;
	}

	this (ref this, this t) // compiles, called as: "S e = S (a, b);"
	{
		x = 10_000;
	}
/*
	this (this r) // does not compile: found 'r' when expecting ')'
	{             // would be equivalent to: "this (S r)"
		x = 100_000;
	}

	this (this, this s) // does not compile: found ',' when expecting ')'
	{                   // would be equivalent to: "this (S, S s)"
		x = 1_000_000;
	}
*/
}

import std.stdio;

void main ()
{
	S a;
	S b = a;
	S c = S (a);
	S d = S (S (a));
	S e = S (a, b);
	writeln (a.x, " ", a.y); // 1 0
	writeln (b.x, " ", b.y); // 10 1
	writeln (c.x, " ", c.y); // 100 0
	writeln (d.x, " ", d.y); // 1000 0
	writeln (e.x, " ", e.y); // 10000 0
}
-----
Now, "this(this)" is the postblit constructor and thus an understandable exclusion.  Still, the reason "this(this r)" or "this(this, this s)" do not currently compile - while other examples compile and run smoothly - seems arbitrary and technical.

To always require using "typeof(this)" instead of "this" for the type would be less confusing.

An intermediate yet not-so-confusing solution would be to forbid using "this" in constructor parameter declarations while still permitting it in other method declarations.
Comment 1 Ivan Kazmenko 2013-02-26 11:27:20 UTC
Discussion which led to this issue report: http://forum.dlang.org/thread/pffhtlwgxwhqbiejzkca@forum.dlang.org
Comment 2 yebblies 2013-11-22 19:58:48 UTC
Hmm, this is caused by issue 2540.  Makes me think 2540 was a mistake, as `alias x = this.foo;` is technically aliasing an expression.
CCing Kenji because he was involved in the discussion.
Comment 3 Andrej Mitrovic 2014-05-02 14:23:57 UTC
Looks like Walter is against changing anything here:
https://issues.dlang.org/show_bug.cgi?id=12228#c4
Comment 4 Nick Treleaven 2018-05-12 13:22:11 UTC
Although this was the earlier bug, I'm marking this as a duplicate: Issue 18228 covers `this` in a parameter list and is now fixed. Issue 12228 is still outstanding.

*** This issue has been marked as a duplicate of issue 18228 ***