Can i rewrite methods in one line?

Ivan Agafonov armadil at yandex.ru
Sun Sep 2 19:12:20 PDT 2012


struct Vector(T, uint size)
{
	static assert(size >= 2 && size <= 4);
	static assert(__traits(isFloating, T);
	
	/// Vector components
	union
	{
		T[size] array = 0;
		struct
		{
			static if(size == 1) T x;
			static if(size == 2) T x, y;
			static if(size == 3) T x, y, z;
			static if(size == 4) T x, y, z, w;
		}
	}

	this (T rhs)			{ array[] = rhs; }
	this (T[] components...)	{ array[] = components[]; }
	this (T* components)		{ array[] = components[0..size]; }
	this (T[size] components)	{ array[] = components[]; }
	this (Vector rhs)		{ array[] = rhs.array[]; }
	
	Vector opUnary(string op) () if (op == "-")
	{
		Vector tmp;
		tmp.array[] = -array[];
		return tmp;
	}

	Vector opBinary(string op) (Vector rhs)
		if(op == "+" || op == "-" || op == "*" || op == "/")
	{
		Vector tmp;
		tmp.array[] = mixin("array[] "~op~" rhs.array[]");
		return tmp;
	}
	
	Vector opBinary(string op) (T rhs)
		if(op == "+" || op == "-" || op == "*" || op == "/")
	{
		Vector tmp;
		tmp.array[] = mixin("array[] "~op~" rhs");
		return tmp;
	}

	ref Vector opOpAssign(string op) (Vector rhs)
		if(op == "+" || op == "-" || op == "*" || op == "/")
	{
		mixin("array[] "~op~"= rhs.array[];");
		return this;
	}

	ref Vector opOpAssign(string op) (T rhs)
		if(op == "+" || op == "-" || op == "*" || op == "/")
	{
		mixin("array[] "~op~"= rhs;");
		return this;
	}
}

I want to rewrite it in something like this:

struct Vector(T, uint size)
{
	static assert(size >= 2 && size <= 4);
	static assert(__traits(isFloating, T);
	
	/// Vector components
	union
	{
		T[size] array = 0;
		struct
		{
			static if(size == 1) T x;
			static if(size == 2) T x, y;
			static if(size == 3) T x, y, z;
			static if(size == 4) T x, y, z, w;
		}
	}

	this (T rhs)			{ array[] = rhs; }
	this (T[] components...)	{ array[] = components[]; }
	this (T* components)		{ array[] = components[0..size]; }
	this (T[size] components)	{ array[] = components[]; }
	this (Vector rhs)		{ array[] = rhs.array[]; }
	
	Vector opUnary(string op) () if (op == "-")
	{ return Vector(-array); }

	Vector opBinary(string op) (Vector rhs)
		if(op == "+" || op == "-" || op == "*" || op == "/")
	{ return Vector(mixin("array[] "~op~" rhs.array[]")); }
	
	Vector opBinary(string op) (T rhs)
		if(op == "+" || op == "-" || op == "*" || op == "/")
	{ return Vector(mixin("array[] "~op~" rhs")); }

	ref Vector opOpAssign(string op) (Vector rhs)
		if(op == "+" || op == "-" || op == "*" || op == "/")
	{ return Vector(mixin("array[] "~op~"= rhs.array[]")); }

	ref Vector opOpAssign(string op) (T rhs)
		if(op == "+" || op == "-" || op == "*" || op == "/")
	{ return Vector(mixin("array[] "~op~"= rhs")); }
}

Main goal is to return and constuct in one place. Can I do so?
I now that there will take place compiller optimisations and both 
versions will be the same, but what if not? PS: Sorry for my 
english.


More information about the Digitalmars-d-learn mailing list