www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - typeof alias in templates

reply bearophile <bearophileHUGS lycos.com> writes:
In a template I need both the name and type of a given variable, but I have
found a problem that can be shown with this simple code:

template Bar(string name) {
    pragma(msg, name);
}
template Foo(alias A) {
    alias Bar!(A.stringof) Foo;
}
void main() {
    int x = 10;
    alias Foo!(x) Nothing;
}

I'd like this code to print "x" instead of "int". Is this a bug of DMD V.1.033?

(At the moment I have solved the problem giving the template the name of the
variable, and then finding its type with typeof(mixin("name")), but it's a less
elegant solution).

Bye,
bearophile
Jul 20 2008
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"bearophile" <bearophileHUGS lycos.com> wrote in message 
news:g5vi9l$1eok$1 digitalmars.com...
 In a template I need both the name and type of a given variable, but I 
 have found a problem that can be shown with this simple code:

 template Bar(string name) {
    pragma(msg, name);
 }
 template Foo(alias A) {
    alias Bar!(A.stringof) Foo;
 }
 void main() {
    int x = 10;
    alias Foo!(x) Nothing;
 }

 I'd like this code to print "x" instead of "int". Is this a bug of DMD 
 V.1.033?

 (At the moment I have solved the problem giving the template the name of 
 the variable, and then finding its type with typeof(mixin("name")), but 
 it's a less elegant solution).
It's got to be a bug. If you put "pragma(msg, A.stringof);" in Foo, you'll notice it prints x and not int. A workaround is, in Foo, to assign A.stringof to a constant and then pass that constant as the template argument to Bar.
Jul 20 2008
prev sibling next sibling parent reply JAnderson <ask me.com> writes:
bearophile wrote:
 In a template I need both the name and type of a given variable, but I have
found a problem that can be shown with this simple code:
 
 template Bar(string name) {
     pragma(msg, name);
 }
 template Foo(alias A) {
     alias Bar!(A.stringof) Foo;
 }
 void main() {
     int x = 10;
     alias Foo!(x) Nothing;
 }
 
 I'd like this code to print "x" instead of "int". Is this a bug of DMD V.1.033?
 
 (At the moment I have solved the problem giving the template the name of the
variable, and then finding its type with typeof(mixin("name")), but it's a less
elegant solution).
 
 Bye,
 bearophile
I imagine once x gets into the template all the compiler knows about it is its type. -Joel
Jul 21 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"JAnderson" <ask me.com> wrote in message 
news:g6286p$20q$1 digitalmars.com...
 bearophile wrote:
 In a template I need both the name and type of a given variable, but I 
 have found a problem that can be shown with this simple code:

 template Bar(string name) {
     pragma(msg, name);
 }
 template Foo(alias A) {
     alias Bar!(A.stringof) Foo;
 }
 void main() {
     int x = 10;
     alias Foo!(x) Nothing;
 }

 I'd like this code to print "x" instead of "int". Is this a bug of DMD 
 V.1.033?

 (At the moment I have solved the problem giving the template the name of 
 the variable, and then finding its type with typeof(mixin("name")), but 
 it's a less elegant solution).

 Bye,
 bearophile
I imagine once x gets into the template all the compiler knows about it is its type.
See my post.
Jul 21 2008
prev sibling parent reply Don <nospam nospam.com.au> writes:
bearophile wrote:
 In a template I need both the name and type of a given variable, but I have
found a problem that can be shown with this simple code:
 
 template Bar(string name) {
     pragma(msg, name);
 }
 template Foo(alias A) {
     alias Bar!(A.stringof) Foo;
 }
 void main() {
     int x = 10;
     alias Foo!(x) Nothing;
 }
 
 I'd like this code to print "x" instead of "int". Is this a bug of DMD V.1.033?
 
 (At the moment I have solved the problem giving the template the name of the
variable, and then finding its type with typeof(mixin("name")), but it's a less
elegant solution).
 
 Bye,
 bearophile
A bug. Workaround: template Foo(alias A) { alias Bar!(A.stringof ~ "") Foo; }
Jul 22 2008
parent bearophile <bearophileHUGS lycos.com> writes:
Don:
 A bug. Workaround:
 template Foo(alias A) {
      alias Bar!(A.stringof ~ "") Foo;
 }
Thank you, this workaround is very short and usable, it avoids me to use another template :-) Bye, bearophile
Jul 22 2008