www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - strange resuts with .stringof and template

reply BCS <ao pathlink.com> writes:
|import std.stdio;
|
|template T(A...){alias A T;}
|struct Baz
|{
|	template Bar(char[] str)
|	{
|		pragma(msg,"B:"~str);
|		bool Bar;
|	}
|}
|
|void main()
|{
|	Baz v;
|	foreach(i,j;T!(1,2,3,4))
|	{
|		static const char[] s = i.stringof;
|		pragma(msg,"1:"~s);
|		v.Bar!(s) = !!(j&0x01);
|		writef("%d, %s : %s\n", i, v.Bar!(i.stringof), !!(j&0x01));
|	}
|	writef("\n");
|	foreach(i,j;T!(1,2,3,4))
|	{
|		static const char[] s = i.stringof;
|		pragma(msg,"2:"~s);
|		writef("%d, %s : %s\n", i, v.Bar!(s), !!(j&0x01));
|	}
|}


compiles outputting:
1:0
B:0
B:uint
1:1
1:2
1:3
2:0
2:1
2:2
2:3

should be
1:0
B:0
1:1
B:1
1:2
B:2
1:3
B:3
2:0
2:1
2:2
2:3

runs giving:
0, true : true
1, false : false
2, true : true
3, false : false

0, false : true
1, false : false
2, false : true
3, false : false
Jul 20 2007
parent Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
BCS wrote:
 
 |import std.stdio;
 |
 |template T(A...){alias A T;}
 |struct Baz
 |{
 |    template Bar(char[] str)
 |    {
 |        pragma(msg,"B:"~str);
 |        bool Bar;
 |    }
 |}
 |
 |void main()
 |{
 |    Baz v;
 |    foreach(i,j;T!(1,2,3,4))
 |    {
 |        static const char[] s = i.stringof;
 |        pragma(msg,"1:"~s);
 |        v.Bar!(s) = !!(j&0x01);
 |        writef("%d, %s : %s\n", i, v.Bar!(i.stringof), !!(j&0x01));
 |    }
 |    writef("\n");
 |    foreach(i,j;T!(1,2,3,4))
 |    {
 |        static const char[] s = i.stringof;
 |        pragma(msg,"2:"~s);
 |        writef("%d, %s : %s\n", i, v.Bar!(s), !!(j&0x01));
 |    }
 |}
 
 
 compiles outputting:
 1:0
 B:0
 B:uint
 1:1
 1:2
 1:3
 2:0
 2:1
 2:2
 2:3
 
 should be
 1:0
 B:0
 1:1
 B:1
 1:2
 B:2
 1:3
 B:3
 2:0
 2:1
 2:2
 2:3
 

This is probably Issue 1168, without looking too closely at your code. -- Remove ".doesnotlike.spam" from the mail address.
Jul 31 2007