www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - strange template syntax

reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
Hello,

some time ago, chris (ruunhb) posted something on his Dspec project, where he
used a template syntax I didn't know:

----
void each(alias array, T : T[] = typeof(array))(void delegate(T item) dg) {
 foreach(T i; array)
   dg(i);
}

 int[] array = [1, 2, 3, 4];
 int b = 10;

 each!(array) = (int item) {
   writefln("%d", item + b);
 };
---

I'm intrigued by the last lines. I'd have thought 'each' to be invoked by:

each!(array)( (int item) {writefln("%d", item+b)} );

But this doesn't work. And I never encountered Chris' syntax before:

template!(someArgs) = moreArgs;

Could some be nice enough to enlighten me? How does that work?


Philippe
Apr 11 2010
next sibling parent reply Robert Clipsham <robert octarineparrot.com> writes:
On 11/04/10 15:48, Philippe Sigaud wrote:
 Hello,

 some time ago, chris (ruunhb) posted something on his Dspec project, where he
 used a template syntax I didn't know:

 ----
 void each(alias array, T : T[] = typeof(array))(void delegate(T item) dg) {
   foreach(T i; array)
     dg(i);
 }

   int[] array = [1, 2, 3, 4];
   int b = 10;

   each!(array) = (int item) {
     writefln("%d", item + b);
   };
 ---

 I'm intrigued by the last lines. I'd have thought 'each' to be invoked by:

 each!(array)( (int item) {writefln("%d", item+b)} );

 But this doesn't work. And I never encountered Chris' syntax before:

 template!(someArgs) = moreArgs;

 Could some be nice enough to enlighten me? How does that work?


 Philippe

When using your method, you have to use: ---- each!(array, typeof(array))((int item) {writefln("%d", item+b)}); ---- (I believe this is a bug, dmd should be able to deduce the type here). As for the syntax, you can do this with any function in D: ---- void foo(int a) { writefln( "%d", a ); } /// Prints 1 foo = 1; ---- I didn't realize this worked for free functions, apparently it does. I think in newer versions of D2 functions like this will have to be marked with property, I don't think dmd currently enforces this though.
Apr 11 2010
next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--000325558e168fb3da0483fb1fdc
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Apr 11, 2010 at 17:01, Robert Clipsham <robert octarineparrot.com>wrote:

 When using your method, you have to use:
 ----
 each!(array, typeof(array))((int item) {writefln("%d", item+b)});
 ----
 (I believe this is a bug, dmd should be able to deduce the type here).

OK. I suppose I'd do: void each(alias array)(void delegate(ElementType!(typeof(array)) item) dg) if (isArray!(typeof(array))) { foreach(T i; array) dg(i); } and then: each!([0,1,2,3])( (int i) { writeln(i);} );
 As for the syntax, you can do this with any function in D:
 ----
 void foo(int a) { writefln( "%d", a ); }
 /// Prints 1
 foo = 1;
 ----
 I didn't realize this worked for free functions, apparently it does. I
 think in newer versions of D2 functions like this will have to be marked
 with  property, I don't think dmd currently enforces this though.

Urgh. OK, I didn't think of properties, thanks a lot Robert ! I think it explains some strange errors I have somewhere else, when trying to assign some value and getting strange unvalid args errors. DMD transforms my foo = something into foo(something). --000325558e168fb3da0483fb1fdc Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><div class=3D"gmail_quote">On Sun, Apr 11, 2010 at 17:01, Robert Clipsh= am <span dir=3D"ltr">&lt;<a href=3D"mailto:robert octarineparrot.com">rober= t octarineparrot.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_qu= ote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 20= 4, 204); padding-left: 1ex;"> <div><div></div>When using your method, you have to use:<br></div> ----<br> each!(array, typeof(array))((int item) {writefln(&quot;%d&quot;, item+b)});= <br> ----<br> (I believe this is a bug, dmd should be able to deduce the type here). </bl= ockquote><div><br>OK.=A0 I suppose I&#39;d do:<br><br>void each(alias array= )(void delegate(ElementType!(typeof(array)) item)=20 dg) if (isArray!(typeof(array))) {<br> =A0foreach(T i; array)<br> =A0 dg(i);<br> }<br><br>and then:<br><br>each!([0,1,2,3])( <br>=A0=A0=A0 (int i) { writeln= (i);}<br>);<br> <br><br>=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt= 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">= As for the syntax, you can do this with any function in D:<br> ----<br> void foo(int a) { writefln( &quot;%d&quot;, a ); }<br> /// Prints 1<br> foo =3D 1;<br> ----<br> I didn&#39;t realize this worked for free functions, apparently it does. I = think in newer versions of D2 functions like this will have to be marked wi= th property, I don&#39;t think dmd currently enforces this though.<br> </blockquote></div><br>Urgh. OK, I didn&#39;t think of properties, thanks a= lot Robert ! <br>I think it explains some strange errors I have somewhere = else, when trying to assign some value and getting strange unvalid args err= ors. DMD transforms my foo =3D something into foo(something).<br> <br><br> --000325558e168fb3da0483fb1fdc--
Apr 11 2010
prev sibling parent Robert Clipsham <robert octarineparrot.com> writes:
On 11/04/10 16:01, Robert Clipsham wrote:
 When using your method, you have to use:
 ----
 each!(array, typeof(array))((int item) {writefln("%d", item+b)});
 ----
 (I believe this is a bug, dmd should be able to deduce the type here).
 As for the syntax, you can do this with any function in D:
 ----
 void foo(int a) { writefln( "%d", a ); }
 /// Prints 1
 foo = 1;
 ----
 I didn't realize this worked for free functions, apparently it does. I
 think in newer versions of D2 functions like this will have to be marked
 with  property, I don't think dmd currently enforces this though.

When I tried again, your method worked... odd, I must have done something wrong before.
Apr 11 2010
prev sibling parent BCS <none anon.com> writes:
Hello Philippe,

 Hello,
 
 some time ago, chris (ruunhb) posted something on his Dspec project,
 where he used a template syntax I didn't know:
 
 ----
 void each(alias array, T : T[] = typeof(array))(void delegate(T item)
 dg) {
 foreach(T i; array)
 dg(i);
 }
 int[] array = [1, 2, 3, 4];
 int b = 10;
 each!(array) = (int item) {
 writefln("%d", item + b);
 };
 ---
 I'm intrigued by the last lines. I'd have thought 'each' to be invoked
 by:
 
 each!(array)( (int item) {writefln("%d", item+b)} );
 

What that is, is the property syntax. I places where there is a function named foo that takes one arg, it can be called by "foo = arg;"
 But this doesn't work. And I never encountered Chris' syntax before:

I'm not sure why the normal syntax wouldn't work. -- ... <IXOYE><
Apr 11 2010