opDispatch shadowing toString - feature or bug?

Damian Ziemba nazriel at driv.pl
Thu Sep 1 00:34:43 PDT 2011


Greetings.

I've been playing around with opDispatch and toString methods and I found 
strange behavior.

Example:

	import std.stdio;

	struct Test
	{
		string opDispatch( string key )()
		{
			return "I am dispatching in struct!";
		}

		string toString()
		{
			return "I am Test struct!";
		}
	}

	class Test2
	{
		string opDispatch( string key )()
		{
			return "I am dispatching in class!";
		}

		string toString()
		{
			return "I am Test class!";
		}
	}

	void main()
	{
		Test test = Test();
		writeln ( test.s ); // I am dispatching in struct!
		writeln ( test.s() ); // I am dispatching in struct!
		writeln ( test ); // NOTHING :( But should return "I am 
Test struct!"

		Test2 test2 = new Test2();
		writeln ( test2.s ); // I am dispatching in class!
		writeln ( test2.s() ); // I am dispatching in class!
		writeln ( test2 ); // NOTHING :( But should return "I am 
Test class!"
	}

Is it a feature or a bug?

Best regards,
Damian Ziemba



More information about the Digitalmars-d-learn mailing list