Re: Get UDAs of self's declaration as a member?

2022-04-25 Thread Ali Çehreli via Digitalmars-d-learn

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



Get UDAs of self's declaration as a member?

2022-04-25 Thread cc via Digitalmars-d-learn
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?