www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using attributes inside template instantiation

reply "Uranuz" <neuranuz gmail.com> writes:
I'm trying to declare format for database record in compile time. 
I consider to use attributes fo this. Can I do something similar 
to this code?

struct RecordFormat(T)
{}

alias Format = RecordFormat!(  ("cool") int );

pragma( msg, Format );
	
void main()
{
}

Or I want something strange that is not supported by language? 
For now I don't understand whether UDA are a part of types or 
they are a part of variable instance definition or something 
different (3rd dimension)).

For example 'const int' is a type but what  attribute int is? Can 
it be considered as separate type or it could be binded to 
definition of variable only?
 attribute int number;

I failed to alias type
alias coolInt =  ("cool") int;

But I can alias const int
alias constInt = const int;

I think I understood something wrong about attributes.
Jun 25 2014
parent reply "Chris Nicholson-Sauls" <ibisbasenji gmail.com> writes:
UDA's are compile-time metadata associated with a specific 
declaration.  So in something like:

 foo int x;

The  foo is attached to x, but is not part of the type.
Jun 25 2014
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Wed, Jun 25, 2014 at 05:10:06PM +0000, Chris Nicholson-Sauls via
Digitalmars-d-learn wrote:
 UDA's are compile-time metadata associated with a specific
 declaration.  So in something like:
 
  foo int x;
 
 The  foo is attached to x, but is not part of the type.
The term "attribute" is a bit confusing, especially since "property" is also used in the language to refer to something completely different. A better term is perhaps "annotation". The foo is an annotation on x, but its type is just int. Furthermore, the foo annotation on it only exists at compile-type; it doesn't exist at runtime. The purpose is really to enhance compile-time introspection and metaprogramming; but what the OP wants seems to be something else altogether. Sadly, the "attribute" terminology has stuck, and is unlikely to change. T -- What do you get if you drop a piano down a mineshaft? A flat minor.
Jun 25 2014
parent reply "Chris Nicholson-Sauls" <ibisbasenji gmail.com> writes:
On Wednesday, 25 June 2014 at 17:21:21 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
 The term "attribute" is a bit confusing, especially since 
 "property" is
 also used in the language to refer to something completely 
 different. A
 better term is perhaps "annotation". The  foo is an annotation 
 on x, but
 its type is just int.
Agree whole-heartedly. I usually introduce people to the idea by referring to them as annotations, which they understand quickly, then later tell them that it's called an attribute because of reasons no one knows. I get funny looks, but at least they comprehend it. I'm not so sure it's too late to get the terminology changed, though, and I sincerely hope it will.
Jun 25 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
But if I write

 ("hello") struct Hello {}

so all of the variables that have type Hello will have attribute 
 ("hello") like come type qualifier because attribute is a part 
of declaration of Hello. Do I understand correctly?
Jun 26 2014
parent "Meta" <jared771 gmail.com> writes:
On Thursday, 26 June 2014 at 07:11:03 UTC, Uranuz wrote:
 But if I write

  ("hello") struct Hello {}

 so all of the variables that have type Hello will have 
 attribute  ("hello") like come type qualifier because attribute 
 is a part of declaration of Hello. Do I understand correctly?
No, it is only the symbol Hello that will have the attribute ("hello"). Attributes are attached to symbols, not types or values.
Jun 26 2014