www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mixin alias

reply Derek Parnell <derek psych.ward> writes:
Is there any reason why the alias template parameter cannot be a literal?


template Foo(alias b) {
   int X = b;
}

void main() {
  int y = 4;
  mixin Foo!(y);  // This is okay
  mixin Foo!(4);  // This fails to compile 
   // "mixin Foo!(4) does not match any template declaration"
}

       
-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
Dec 16 2007
next sibling parent 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
Derek Parnell wrote:
 Is there any reason why the alias template parameter cannot be a literal?
 
 
 template Foo(alias b) {
    int X = b;
What happens when "b=3;"?
 }
 
 void main() {
   int y = 4;
   mixin Foo!(y);  // This is okay
   mixin Foo!(4);  // This fails to compile 
    // "mixin Foo!(4) does not match any template declaration"
 }
 
        
regards, frank
Dec 16 2007
prev sibling next sibling parent Aarti <aarti interia.pl> writes:
Derek Parnell pisze:
 Is there any reason why the alias template parameter cannot be a literal?
 
 
 template Foo(alias b) {
    int X = b;
 }
 
 void main() {
   int y = 4;
   mixin Foo!(y);  // This is okay
   mixin Foo!(4);  // This fails to compile 
    // "mixin Foo!(4) does not match any template declaration"
 }
 
        
IMHO should work... Bug? BR Marcin Kuszczak (aarti_pl)
Dec 16 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Derek Parnell" <derek psych.ward> wrote in message 
news:jnak6l8ihhe1.k3tzm3in813z.dlg 40tude.net...
 Is there any reason why the alias template parameter cannot be a literal?


 template Foo(alias b) {
   int X = b;
 }

 void main() {
  int y = 4;
  mixin Foo!(y);  // This is okay
  mixin Foo!(4);  // This fails to compile
   // "mixin Foo!(4) does not match any template declaration"
 }
This is correct. You can only alias things that have names; the number 3 does not have a name. What's weird is that: template Foo(alias b) { int X = b; } template Foo(int b) { int X = b; } void main() { int y = 4; mixin Foo!(4); // OK // mixin Foo!(y); // fails, matches multiple (???) } doesn't. Why does Foo!(y) match Foo(int)? A (kind of dumb) workaround is: template Foo(b...) { static assert(b.length == 1); int X = b[0]; }
Dec 16 2007
parent reply Derek Parnell <derek psych.ward> writes:
On Sun, 16 Dec 2007 15:51:38 -0500, Jarrett Billingsley wrote:

 "Derek Parnell" <derek psych.ward> wrote in message 
 news:jnak6l8ihhe1.k3tzm3in813z.dlg 40tude.net...
 Is there any reason why the alias template parameter cannot be a literal?


 template Foo(alias b) {
   int X = b;
 }

 void main() {
  int y = 4;
  mixin Foo!(y);  // This is okay
  mixin Foo!(4);  // This fails to compile
   // "mixin Foo!(4) does not match any template declaration"
 }
This is correct. You can only alias things that have names; the number 3 does not have a name.
I know that is 'correct', but I was really asking what is the rationale behind such an apparently arbitrary design decision.
 What's weird is that:
 
 template Foo(alias b)
 {
     int X = b;
 }
 
 template Foo(int b)
 {
     int X = b;
 }
 
 void main()
 {
     int y = 4;
     mixin Foo!(4); // OK
     // mixin Foo!(y); // fails, matches multiple (???)
 }
 
 doesn't.  Why does Foo!(y) match Foo(int)?
 
 A (kind of dumb) workaround is:
 
 template Foo(b...)
 {
     static assert(b.length == 1);
     int X = b[0];
 }
There are still a lot of things re templates that strike me as eccentric. -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 16 2007
next sibling parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Mon, 17 Dec 2007 03:07:22 +0100, 0ffh wrote:

 Derek Parnell wrote:
 On Sun, 16 Dec 2007 15:51:38 -0500, Jarrett Billingsley wrote:
 
 "Derek Parnell" <derek psych.ward> wrote in message 
 news:jnak6l8ihhe1.k3tzm3in813z.dlg 40tude.net...
 Is there any reason why the alias template parameter cannot be a literal?


 template Foo(alias b) {
   int X = b;
 }

 void main() {
  int y = 4;
  mixin Foo!(y);  // This is okay
  mixin Foo!(4);  // This fails to compile
   // "mixin Foo!(4) does not match any template declaration"
 }
This is correct. You can only alias things that have names; the number 3 does not have a name.
I know that is 'correct', but I was really asking what is the rationale behind such an apparently arbitrary design decision.
Well, for one we'd have to disallow assignment expressions using the aliased parameter (as well as pre- and post-increment)
Why? If you had such a template (one that used 'side effects') but supplied a literal, it would/should fail at the mixin site. I can't see why that necessarily prevents the same template being used with non-literals, as your example below demonstrates.
 template Foo(alias b)
 {
    int X=(b=4);
 }
 
 void main()
 {
    int a=3;
    printf("%i\n",a); // 3
    mixin Foo!(a);    // side effect here
    printf("%i\n",a); // yeas, it's 4 now
    printf("%i\n",X); // 4
mixin Foo!(5) // Should fail because 'int X=(5=4);' is bad syntax.
 }
IMHO, such a template needs *heavy* justification as do all such 'hidden' side effects code. Buts that's just the way I think. -- Derek (skype: derek.j.parnell) Melbourne, Australia 17/12/2007 1:27:49 PM
Dec 16 2007
parent 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
Derek Parnell wrote:
 Why? If you had such a template (one that used 'side effects') but supplied
 a literal, it would/should fail at the mixin site. I can't see why that
 necessarily prevents the same template being used with non-literals, as
 your example below demonstrates.
You are right. I had the post retracted, but it seems you've seen it too quick! =)
 template Foo(alias b)
 {
    int X=(b=4);
 }
 [...]
mixin Foo!(5) // Should fail because 'int X=(5=4);' is bad syntax.
Yes, as it does for int instead alias, quite right there!
 IMHO, such a template needs *heavy* justification as do all such 'hidden'
 side effects code. Buts that's just the way I think.
I agree, but as it is possible it would still have to be considered. regards, frank
Dec 16 2007
prev sibling parent reply 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
Derek Parnell wrote:
 On Sun, 16 Dec 2007 15:51:38 -0500, Jarrett Billingsley wrote:
 This is correct.  You can only alias things that have names; the number 3 
 does not have a name.
I know that is 'correct', but I was really asking what is the rationale behind such an apparently arbitrary design decision.
First I thought: "Well, for one we'd have to disallow assignment expressions using the aliased parameter (as well as pre/post-inc/decrement), which is bad for the goal of context insensitive scanning." But that's not it... same would go for "template Foo(int b)" where the error is caught later. Therefore I can only conjecture that it would add some complication to make some constant variable magically spring to life in order to replace the literal in the template code, and nobody has yet bothered Walter enough to do it.
 Why does Foo!(y) match Foo(int)?
JB: As y is declared as an int, I don't see why not... otherwise the template would only react to int literals but not int veriables, no? But I don't use templating that much, and so may overlook something. regards, frank
Dec 16 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"0ffh" <frank frankhirsch.youknow.what.todo.net> wrote in message 
news:fk4nmj$7ad$1 digitalmars.com...

 JB: As y is declared as an int, I don't see why not... otherwise the
 template would only react to int literals but not int veriables, no?
 But I don't use templating that much, and so may overlook something.
Except that template value parameters have to be compile-time constants.. unless the compiler's trying to be "smart" here, which I honestly did not expect.
Dec 16 2007