www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - mixin functions

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d0420a695051f6404cd710eb6
Content-Type: text/plain; charset=UTF-8

I find myself doing this very very often and it pains me to write ugly code
like this over and over again:

mixin(()=>{

    string result;
    foreach(i; 0..10)
        result ~= "writeln(" ~ to!string(i); ~ ");\n"'
    return result;

}());

All it does is generates a string in a delegate, which is immediately
called and passed to a mixin.
Almost all mixins contain generated strings and most if them need a
dedicated string generator.
I don't want to propose new syntax, because we all know that new syntax is
the last thing that will be developed, considering the number of bugs out
there. The first thing I wanted to do was this:

mixin
{
    foreach(i; 0..10)
        mixin ~= "writeln(" ~ to!string(i); ~ ");\n"'
}

I'm not suggesting this to be implemented, this is just what I
automatically wanted to write.
Anybody else had this kind of thoughts?

-- 
Bye,
Gor Gyolchanyan.

--f46d0420a695051f6404cd710eb6
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

I find myself doing this very very often and it pains me to write ugly code=
 like this over and over again:<br clear=3D"all"><div><br></div><div>mixin(=
()=3D&gt;{</div><div><br></div><div>=C2=A0 =C2=A0 string result;</div><div>=
=C2=A0 =C2=A0 foreach(i; 0..10)</div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 result ~=3D &quot;writeln(&quot; ~ to!stri=
ng(i); ~ &quot;);\n&quot;&#39;</div><div>=C2=A0 =C2=A0 return result;</div>=
<div><br></div><div>}());</div><div><br></div><div>All it does is generates=
 a string in a delegate, which is immediately called and passed to a mixin.=
</div>
<div>Almost all mixins contain generated strings and most if them need a de=
dicated string generator.</div><div>I don&#39;t want to propose new syntax,=
 because we all know that new syntax is the last thing that will be develop=
ed, considering the number of bugs out there. The first thing I wanted to d=
o was this:</div>
<div><br></div><div>mixin</div><div>{</div><div><div>=C2=A0 =C2=A0 foreach(=
i; 0..10)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 mixin ~=3D &quot;writeln(&q=
uot; ~ to!string(i); ~ &quot;);\n&quot;&#39;</div>}</div><div><br></div><di=
v>I&#39;m not suggesting this to be implemented, this is just what I automa=
tically wanted to write.</div>
<div>Anybody else had this kind of thoughts?</div><div><br></div>-- <br>Bye=
,<br>Gor Gyolchanyan.<br>

--f46d0420a695051f6404cd710eb6--
Nov 01 2012
next sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
 I'm not suggesting this to be implemented, this is just what I
 automatically wanted to write.
 Anybody else had this kind of thoughts?

I think that code generating mixin strings should be as well designed and documented as every other code you write. This mixin feature would be just another step to gain C's preprocessor's reputation for string mixins.
Nov 01 2012
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-11-01 16:55, Gor Gyolchanyan wrote:
 I find myself doing this very very often and it pains me to write ugly
 code like this over and over again:

 mixin(()=>{

      string result;
      foreach(i; 0..10)
          result ~= "writeln(" ~ to!string(i); ~ ");\n"'
      return result;

 }());

 All it does is generates a string in a delegate, which is immediately
 called and passed to a mixin.
 Almost all mixins contain generated strings and most if them need a
 dedicated string generator.
 I don't want to propose new syntax, because we all know that new syntax
 is the last thing that will be developed, considering the number of bugs
 out there. The first thing I wanted to do was this:

 mixin
 {
      foreach(i; 0..10)
          mixin ~= "writeln(" ~ to!string(i); ~ ");\n"'
 }

 I'm not suggesting this to be implemented, this is just what I
 automatically wanted to write.
 Anybody else had this kind of thoughts?

I would vote for AST macros instead. -- /Jacob Carlborg
Nov 01 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Nov 01, 2012 at 08:31:30PM +0100, Jacob Carlborg wrote:
 On 2012-11-01 16:55, Gor Gyolchanyan wrote:
I find myself doing this very very often and it pains me to write
ugly code like this over and over again:

mixin(()=>{

     string result;
     foreach(i; 0..10)
         result ~= "writeln(" ~ to!string(i); ~ ");\n"'
     return result;

}());

All it does is generates a string in a delegate, which is immediately
called and passed to a mixin.
Almost all mixins contain generated strings and most if them need a
dedicated string generator.
I don't want to propose new syntax, because we all know that new
syntax is the last thing that will be developed, considering the
number of bugs out there. The first thing I wanted to do was this:


 I would vote for AST macros instead.

Yeah, mixins are a kind of hack that's unfortunately rather widely used right now. A proper AST macro system would be much better. But I'm not holding my breath for it, given how many issues we still have to fix with our current feature set. T -- Indifference will certainly be the downfall of mankind, but who cares? -- Miquel van Smoorenburg
Nov 01 2012