Operators overloading in D2

Dan daniele.niero at gmail.com
Sat Apr 24 02:07:41 PDT 2010


Hi All,

I just downloaded D2 after a friend of mine told me about it and I was playing with it, just to get confident with the
language.

In order to do that I was converting a simple geometric Vector3 class I wrote in c++.
this is the (relavant for this post) D code

class Vector3
{
	float x=0.0;
	float y=0.0;
	float z=0.0;

	this() {}

	this(float X, float Y, float Z)
	{
		x=X; y=Y; z=Z;
	}

	void opOpAssign(string op:"+=")(ref Vector3 other)
	{
		x += other.x;
		y += other.y;
		z += other.z;
	}
}


I struggled immediately with operators overloading and I don't really get why they changed so drastically from D1.

for example
void opOpAssign(string op:"+=")(ref Vector3 other)
was
void opAddAssign(ref Vector3 other)

I'm not an hard core programmer and what I do is always high level or probably I worked a little to much with Python,
but the old way to overload operators looks to me much more easy to understand.

So there's my questions
Why D2 changed in this way the operators overloading?
I saw the the compiler compiles both the functions, even considering this I assume it's not safe to use the old D1 way,
right?

Oh, and I've found the documentation really confusing on this metter.

Thanks,
    Dan




More information about the Digitalmars-d-learn mailing list