2

Is it possible using D's builtin traits and/or std.traits to detect whether a function (either within a class/struct or without) is annotated with @property? I know @property isn't really an attribute, but I thought __traits(getAttributes, ...) might do it (no such luck).

Meta
  • 1,091
  • 6
  • 14
  • Why do you need it? When in doubt - use parenthesis! – Idan Arye Jun 24 '13 at 13:40
  • You can even call functions without `@property` without parenthesis, so I don't see an actual need for that. – dav1d Jun 24 '13 at 14:19
  • I'm attempting to write a template function that generates a "builder" class for an arbitrary class. In my project I have been using @property strictly as setters/getters, so the idea is to get all of the functions from a class annotated with the property attribute, and then generate the builder methods. – Meta Jun 24 '13 at 14:39
  • 1
    Doesn't std.traits.functionAttributes work in that case? – jpf Jun 24 '13 at 15:23

1 Answers1

4

In general, I would think that it would be better to just test that the expression you want to use compiles (e.g. is(typeof(var.prop))), but if for some reason you really need to know that a function is marked with @property, then you can use std.traits.functionAttributes to get that information. Something like

enum isProperty = (functionAttributes!(MyType.prop) & FunctionAttribute.property) != 0;

should work.

Jonathan M Davis
  • 37,181
  • 17
  • 72
  • 102