Static operator overloads

Stuart Murray stuart.w.murray at fakey.spambot.avoiding.gmail.com
Mon Jun 4 22:47:57 PDT 2007


Hi everyone
I'm a newbie to D and I've spent some time in the last few days messing around, seeing how things work.

I noticed in a news posting that you can overload operators statically..
i.e.

///////////////

class Test
{
	private char[] memstring;
	
	this(char[] newstring)
	{
		memstring = newstring.dup;
	}
	
	char[] toString()
	{
		return memstring;
	}
	
	static Test opCat(char[] newstring)
	{
		return new Test(newstring);
	}
	
/+
	Test opCat(char[] newstring)
	{
		return new Test(memstring ~ newstring);
	}
+/
}

int main(char[][] args)
{
	Test bob = Test ~ "hello there";
	writefln(bob);
	return 0;
}

////////////////

Browsing through the documentation I didn't come across any mention that static member operators were supported. I was just wondering if they are in fact intentional?

Also:
I notice that having 2 member functions with the same signature, one static and one not, does not work (compiler claims it a conflicts). In particular, the above case, when the non-static opCmp is uncommented it will no longer compile.

Is there any notes in the documentation to this effect? because I could not find any.

Thanks for any reply.



More information about the Digitalmars-d-learn mailing list