String and opBinary

Eyyub eyyub.pangearaion at gmail.com
Fri Jun 1 10:51:35 PDT 2012


Hi,

I'd like to know why this following code doesn't compile :

string opBinary(string op : "*")(string data, string word)
{
	string temp;
	foreach(letter; word)
	{
		temp ~= data;
	}
	return temp;
}

void main()
{
	string word = "foo";
	string secretword = "_" * word; //Error: '"_"' is not of 
arithmetic type                            it is a string
         //Error: 'word' is not of arithmetic type, it is a string
	string secretword = "_".opBinary!"*"(word); // compile
	writeln(secretword); // output : ___
}

Even more weird, with just another op specialisation but the same 
function's body :
string opBinary(string op : "+")(string data, string word)
{
	string temp;
	foreach(letter; word)
	{
		temp ~= data;
	}
	return temp;
}

void main()
{
	string word = "foo";
	string secretword = "_" + word; // Error: Array operation "_" - 
word not implemented // why the error message is not the same 
with different op ?
	string secretword = "_".opBinary!"+"(word); // this works fine
}

Thanks,

(sorry for my bad english)


More information about the Digitalmars-d-learn mailing list