www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting .init of a Typetuple

reply Johannes Pfau <spam example.com> writes:
Hi,
I want to do exactly the same as described in
http://d.puremagic.com/issues/show_bug.cgi?id=4536 . The problem is I
can't even get the workaround to work. Dmd complains about the following
template:
---------------------------------------------------------------
template Init(T...)
{
    alias (Tuple!T.init).expand Init;
}
---------------------------------------------------------------

Dmd output:
---------------------------------------------------------------
test.d(18): basic type expected, not (
test.d(18): found '!' when expecting ')'
test.d(18): semicolon expected to close alias declaration
test.d(18): no identifier for declarator T.init
test.d(18): semicolon expected, not ')'
test.d(18): Declaration expected, not ')'
---------------------------------------------------------------

Is it possible that this has recently stopped working? Is this a bug in
dmd or is this the expected behavior? Is there any other way to achieve
the same thing?
-- 
Johannes Pfau
Aug 19 2010
next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Johannes Pfau <spam example.com> wrote:

 Hi,
 I want to do exactly the same as described in
 http://d.puremagic.com/issues/show_bug.cgi?id=4536 . The problem is I
 can't even get the workaround to work. Dmd complains about the following
 template:
 ---------------------------------------------------------------
 template Init(T...)
 {
     alias (Tuple!T.init).expand Init;
 }
 ---------------------------------------------------------------

 Dmd output:
 ---------------------------------------------------------------
 test.d(18): basic type expected, not (
 test.d(18): found '!' when expecting ')'
 test.d(18): semicolon expected to close alias declaration
 test.d(18): no identifier for declarator T.init
 test.d(18): semicolon expected, not ')'
 test.d(18): Declaration expected, not ')'
 ---------------------------------------------------------------

 Is it possible that this has recently stopped working? Is this a bug in
 dmd or is this the expected behavior? Is there any other way to achieve
 the same thing?

This works for me: template Init( T ) { alias TypeTuple!( T.init ) Init; } template Init( T, U... ) { alias TypeTuple!( T.init, Init!U ) Init; } -- Simen
Aug 19 2010
parent Johannes Pfau <spam example.com> writes:
On 19.08.2010 22:07, Philippe Sigaud wrote:
 And, looking in my codebase, here is what I'm using ;)
 
 template Init(T...)
 {
     T Init;
 }
 
 It's so simple...
 I think I found this before the bug report and then forgot about it and
 copied an old version. I'll update the bug report accordingly, if the
 latter version works for you.

Great, that works! Thanks for the quick reply. -- Johannes Pfau
Aug 20 2010
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--0015175cd4500f7502048e32ba16
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Aug 19, 2010 at 21:51, Simen kjaeraas <simen.kjaras gmail.com>wrote:

 Johannes Pfau <spam example.com> wrote:

  Hi,
 I want to do exactly the same as described in
 http://d.puremagic.com/issues/show_bug.cgi?id=4536 . The problem is I
 can't even get the workaround to work. Dmd complains about the following
 template:
 ---------------------------------------------------------------
 template Init(T...)
 {

alias (Tuple!T.init).expand Init;
 }


used. Anyway, Simen's solutions is better, a bit less dependency. TypeTuple is a bit more universal than Tuple. Simen:
 template Init( T ) {
    alias TypeTuple!( T.init ) Init;
 }

 template Init( T, U... ) {
    alias TypeTuple!( T.init, Init!U ) Init;
 }

template Init(T...) { T Init; } It's so simple... I think I found this before the bug report and then forgot about it and copied an old version. I'll update the bug report accordingly, if the latter version works for you. Philippe --0015175cd4500f7502048e32ba16 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Thu, Aug 19, 2010 at 21:51, Simen kja= eraas <span dir=3D"ltr">&lt;<a href=3D"mailto:simen.kjaras gmail.com">simen= .kjaras gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote= " style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, = 204); padding-left: 1ex;"> <div><div></div><div class=3D"h5">Johannes Pfau &lt;<a href=3D"mailto:spam = example.com" target=3D"_blank">spam example.com</a>&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> Hi,<br> I want to do exactly the same as described in<br> <a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D4536" target=3D"= _blank">http://d.puremagic.com/issues/show_bug.cgi?id=3D4536</a> . The prob= lem is I<br> can&#39;t even get the workaround to work. Dmd complains about the followin= g<br> template:<br> ---------------------------------------------------------------<br> template Init(T...)<br> { =A0=A0</blockquote></div></div></blockquote><blockquote class=3D"gmail_quo= te" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204= , 204); padding-left: 1ex;"><div><div class=3D"h5"><blockquote class=3D"gma= il_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(20= 4, 204, 204); padding-left: 1ex;"> =A0alias (Tuple!T.init).expand Init;<br> }<br></blockquote></div></div></blockquote><div><br>Hmm, I&#39;m pretty sur= e it used to worked, because as one time that&#39;s what I used.<br><br>Any= way, Simen&#39;s solutions is better, a bit less dependency. TypeTuple is a= bit more universal than Tuple.<br> <br>Simen:<br></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= ;"> <br> template Init( T ) {<br> =A0 =A0alias TypeTuple!( T.init ) Init;<br> }<br> <br> template Init( T, U... ) {<br> =A0 =A0alias TypeTuple!( T.init, Init!U ) Init;<br> }<br><br></blockquote><div><br>And, looking in my codebase, here is what I&= #39;m using ;)<br><br>template Init(T...)<br>{<br>=A0=A0=A0 T Init;<br>}<br=
<br>It&#39;s so simple... <br>I think I found this before the bug report a=

report accordingly, if the latter version works for you.<br> <br>Philippe<br><br></div></div> --0015175cd4500f7502048e32ba16--
Aug 19 2010
prev sibling next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Philippe Sigaud <philippe.sigaud gmail.com> wrote:

 And, looking in my codebase, here is what I'm using ;)

 template Init(T...)
 {
     T Init;
 }

Doh. I believe this is slightly better, though: template Init( T... ) { enum T Init; } :p -- Simen
Aug 19 2010
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--001636c5bb030acd1a048e33109b
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Aug 19, 2010 at 22:16, Simen kjaeraas <simen.kjaras gmail.com>wrote:

 Philippe Sigaud <philippe.sigaud gmail.com> wrote:

  And, looking in my codebase, here is what I'm using ;)
 template Init(T...)
 {
    T Init;
 }

Doh. I believe this is slightly better, though: template Init( T... ) { enum T Init; } :p

What, that's *five* more characters, five! I win, I win! More seriously, yours might be more 'solid', but isn't enum implicit in this case? Anyway, T.init should exist, since "T Init;" works... Philippe --001636c5bb030acd1a048e33109b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Thu, Aug 19, 2010 at 22:16, Simen kja= eraas <span dir=3D"ltr">&lt;<a href=3D"mailto:simen.kjaras gmail.com">simen= .kjaras gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote= " style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, = 204); padding-left: 1ex;"> <div class=3D"im">Philippe Sigaud &lt;<a href=3D"mailto:philippe.sigaud gma= il.com" target=3D"_blank">philippe.sigaud gmail.com</a>&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> And, looking in my codebase, here is what I&#39;m using ;)<br> <br> template Init(T...)<br> {<br> =A0 =A0T Init;<br> }<br> </blockquote> <br></div> Doh. I believe this is slightly better, though:<br> <br> template Init( T... ) {<br> =A0 =A0enum T Init;<br> }<br> <br> :p<br></blockquote><div><br>What, that&#39;s *five* more characters, five! = I win, I win!<br><br>More seriously, yours might be more &#39;solid&#39;, b= ut isn&#39;t enum implicit in this case?<br><br>Anyway, T.init should exist= , since &quot;T Init;&quot; works...<br> <br>Philippe<br><br></div></div> --001636c5bb030acd1a048e33109b--
Aug 19 2010
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Philippe Sigaud <philippe.sigaud gmail.com> wrote:

 What, that's *five* more characters, five! I win, I win!

;'(
 More seriously, yours might be more 'solid', but isn't enum implicit in  
 this
 case?

Seems you are right. I thought the template would work as a namespace, giving this situation: Init!int = 4; int a = Init!int; // What, 4?! But such is not the case.
 Anyway, T.init should exist, since "T Init;" works...

Indeed. -- Simen
Aug 19 2010