www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - UDA question

reply "Ulf Johansson" <ulf.johansson o-vik.com> writes:
I have a question regarding using a struct value as user-defined 
attribute. The use-case I am trying to get working is as follows:

struct message {
     int atom;
}

...

class AnActor : ... {
      message(id) void myHandler(string arg1, real arg2) {
     }

     mixin ... // a mixin that builds message dispatch
}


The 'message' attribute defines methods of the class that will 
participate in message dispatch argument matching. This works 
fine but I want to take this one step further. I want to be able 
to give a message and a handler an ID that more strictly directs 
a message (and gives an error if arguments does not match).

The problem I have is quite simple - getting the value of 'atom' 
out of the message attribute. How can I do this? I did not post 
any of my actual code, I hope the above example describes the 
problem.

Regards,
Ulf
Apr 15 2013
next sibling parent "Dicebot" <m.strashun gmail.com> writes:
On Tuesday, 16 April 2013 at 06:29:22 UTC, Ulf Johansson wrote:
 I have a question regarding using a struct value as 
 user-defined attribute. The use-case I am trying to get working 
 is as follows:

 struct message {
     int atom;
 }

 ...

 class AnActor : ... {
      message(id) void myHandler(string arg1, real arg2) {
     }

     mixin ... // a mixin that builds message dispatch
 }


 The 'message' attribute defines methods of the class that will 
 participate in message dispatch argument matching. This works 
 fine but I want to take this one step further. I want to be 
 able to give a message and a handler an ID that more strictly 
 directs a message (and gives an error if arguments does not 
 match).

 The problem I have is quite simple - getting the value of 
 'atom' out of the message attribute. How can I do this? I did 
 not post any of my actual code, I hope the above example 
 describes the problem.

 Regards,
 Ulf

foreach(attribute, __traits(getAttributes, AnActor)) { static if (is(typeof(attribute) == message)) pragma(msg, attribute.atom); }
Apr 16 2013
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-04-16 08:29, Ulf Johansson wrote:
 I have a question regarding using a struct value as user-defined
 attribute. The use-case I am trying to get working is as follows:

 struct message {
      int atom;
 }

 ...

 class AnActor : ... {
       message(id) void myHandler(string arg1, real arg2) {
      }

      mixin ... // a mixin that builds message dispatch
 }


 The 'message' attribute defines methods of the class that will
 participate in message dispatch argument matching. This works fine but I
 want to take this one step further. I want to be able to give a message
 and a handler an ID that more strictly directs a message (and gives an
 error if arguments does not match).

 The problem I have is quite simple - getting the value of 'atom' out of
 the message attribute. How can I do this? I did not post any of my
 actual code, I hope the above example describes the problem.

 Regards,
 Ulf

Use __traits(getAttributes, symbol) to get the attributes out of the given symbol. It will return a tuple with the attributes. I would guess it would look something like this: message msg = __traits(getAttributes, AnActor.myHandler)[0]; int atom = msg.atom; See the docs: http://dlang.org/traits.html#getAttributes http://dlang.org/attribute.html#uda -- /Jacob Carlborg
Apr 16 2013
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 04/16/2013 08:29 AM, Ulf Johansson wrote:
 ...

 The problem I have is quite simple - getting the value of 'atom' out of
 the message attribute. How can I do this? I did not post any of my
 actual code, I hope the above example describes the problem.
 ...

struct message{ int atom; } class AnActor{ message(3) void myHandler(string arg1, real arg2){ } static assert(__traits(getAttributes, myHandler)[0].atom==3); } (Consider posting to d.D.learn for basic questions.)
Apr 16 2013
prev sibling parent "Ulf Johansson" <ulf.johansson o-vik.com> writes:
On Tuesday, 16 April 2013 at 06:29:22 UTC, Ulf Johansson wrote:
 I have a question regarding using a struct value as 
 user-defined attribute. The use-case I am trying to get working 
 is as follows:

 struct message {
     int atom;
 }

 ...

 class AnActor : ... {
      message(id) void myHandler(string arg1, real arg2) {
     }

     mixin ... // a mixin that builds message dispatch
 }


 The 'message' attribute defines methods of the class that will 
 participate in message dispatch argument matching. This works 
 fine but I want to take this one step further. I want to be 
 able to give a message and a handler an ID that more strictly 
 directs a message (and gives an error if arguments does not 
 match).

 The problem I have is quite simple - getting the value of 
 'atom' out of the message attribute. How can I do this? I did 
 not post any of my actual code, I hope the above example 
 describes the problem.

 Regards,
 Ulf

Thanks, I have it working now! I was getting some strange errors and I guess I needed to know that it could be done. I will use the learn forum next time :)
Apr 16 2013