String and opBinary
Zhenya
zheny at list.ru
Fri Jun 1 12:26:04 PDT 2012
On Friday, 1 June 2012 at 17:51:36 UTC, Eyyub wrote:
> 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)
Hi,Eyyub
//Operator overloading is accomplished by rewriting operators
whose operands //are class or struct objects into calls to
specially named member functions
I'm not sure,but problem may be that opBinary should be member
function
More information about the Digitalmars-d-learn
mailing list