String and opBinary

Dmitry Olshansky dmitry.olsh at gmail.com
Fri Jun 1 12:39:57 PDT 2012


On 01.06.2012 23:37, Zhenya wrote:
> This code work
>
> import std.stdio;
>
> struct String
> {
> string data;
> alias data this;
> this(string s)
> {
> data = s;
> }
> String opBinary(string op : "*")(string word)
> {
> string temp;
> foreach(letter; word)
> {
> temp ~= data;
> }
> return String(temp);
> }
> };
>
> string opBinary(string op : "*")(string data, string word)
> {
> string temp;
> foreach(letter; word)
> {
> temp ~= data;
> }
> return temp;
> }
>
> void main()
> {
> String word = "foo";
> String secretword = String("_") * word;

Define proper opBinaryRight and this will work too:
String secretword = "_" * word;

> //string secretword = "_".opBinary!"*"(word); // compile
> writeln(secretword); // output : ___
> }
>
>


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list