Should this be a compiler error?

Mike L. mike.linford at gmail.com
Sun Jun 7 20:40:40 PDT 2009


The following code compiles fine for me with dmd 1.043:

----------
module wtf;

version(Tango)
	import tango.io.Stdout;
else
	import std.stdio;

interface Printer
{
	void print();
}

class WTF
{
	Printer p;
	
	this(int x)
	{
		p = new class Printer
		{
			override void print()
			{
				version(Tango)
					Stdout(x).newline();
				else
					writefln(x);
			}
		};
	}
}

void main()
{
	WTF wtf = new WTF(42);
	wtf.p.print();
}
----------

The anonymous class is given a method that prints the int x, however, by the time wtf.p.print() is called, x has gone out of scope. It's still possible to call the print(), though, and it just prints garbage. Should this be a compiler error?



More information about the Digitalmars-d mailing list