digitalmars.D.learn - Get UDAs of self's declaration as a member?
- cc (30/30) Apr 25 2022 Hard to word this question right, but is it possible to get the
- =?UTF-8?Q?Ali_=c3=87ehreli?= (26/29) Apr 25 2022 That sounds backwards to me too. :) Policy-based design can work here:
Hard to word this question right, but is it possible to get the UDAs assigned to a class/structure's member variable declaration, within that variable's definition? e.g. ```d import std.stdio; import std.traits; enum SPECIAL; struct Foo { void foo() { static if (hasUDA!(typeof(this), SPECIAL)) writeln("special"); else writeln("not special"); } } struct Bar { SPECIAL Foo foo; } void main() { Foo foo; foo.foo; Bar bar; bar.foo.foo; } ``` This doesn't work of course, ` SPECIAL` isn't applied to `struct Foo` itself so no UDA is found by `hasUDA!Foo`. Without iterating Bar directly, is there some way to detect *within* Foo's member functions, that the Foo being called is declared with ` SPECIAL` inside its parent structure?
Apr 25 2022
On 4/25/22 14:32, cc wrote:Hard to word this question right, but is it possible to get the UDAs assigned to a class/structure's member variable declaration, within that variable's definition? e.g.That sounds backwards to me too. :) Policy-based design can work here: import std.stdio; import std.traits; enum SPECIAL { no, yes } struct Foo(SPECIAL special = SPECIAL.no) { void foo() { static if (special == SPECIAL.yes) writeln("special"); else writeln("not special"); } } struct Bar { Foo!(SPECIAL.yes) foo; } alias FooSpecial = Foo!(SPECIAL.yes); alias FooRegular = Foo!(SPECIAL.no); void main() { Foo!() foo; // <-- Without the aliases FooSpecial foo_; // <-- Better syntax with aliases foo.foo; Bar bar; bar.foo.foo; } Ali
Apr 25 2022