www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - UDA strange behavior

reply "Temtaime" <temtaime gmail.com> writes:
 (3) int a;
enum tp = __traits(getAttributes, a);
				
enum b = tp[0];
writeln(b);

Compiler doesn't accept enum b = tp[0] and says to me:
Error: variable _tp_field_0 cannot be read at compile time

Why? And writeln(tp[0]) works as expected.
Jun 09 2013
next sibling parent reply David <d dav1d.de> writes:
Am 09.06.2013 11:15, schrieb Temtaime:
  (3) int a;
 enum tp = __traits(getAttributes, a);
                
 enum b = tp[0];
 writeln(b);
 
 Compiler doesn't accept enum b = tp[0] and says to me:
 Error: variable _tp_field_0 cannot be read at compile time
 
 Why? And writeln(tp[0]) works as expected.
It works like that: http://dpaste.dzfl.pl/809de989 (3) int a; enum tp = [__traits(getAttributes, a)]; enum b = tp[0]; pragma(msg, b); The exact reason why you have to expand the tuple into an array, I don't know, or can't remember ;)
Jun 09 2013
parent reply "Temtaime" <temtaime gmail.com> writes:
Oh, thanks. But then that code doesn't work:
http://dpaste.dzfl.pl/262b966c
Jun 09 2013
parent "Temtaime" <temtaime gmail.com> writes:
Thanks for all, it's unactual anymore.
Jun 09 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-06-09 11:15, Temtaime wrote:
  (3) int a;
 enum tp = __traits(getAttributes, a);

 enum b = tp[0];
 writeln(b);

 Compiler doesn't accept enum b = tp[0] and says to me:
 Error: variable _tp_field_0 cannot be read at compile time

 Why? And writeln(tp[0]) works as expected.
__traits(getAttributes) returns a tuple. Try using an alias instead, something like: import std.typetuple alias tp = TypeTuple!(__traits(getAttributes, a)); -- /Jacob Carlborg
Jun 09 2013