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
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 reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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;
 }
Hmm, I'm pretty sure it used to worked, because as one time that's what I 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;
 }
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. Philippe
Aug 19 2010
next sibling parent reply "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
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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
Aug 19 2010
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
prev sibling 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