www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Variable notation

reply Joan <paras456azerionet gmail.com> writes:
```import std.stdio;

enum DO_NOT_CHECK;

class Test {
     int n;
     int x;
      DO_NOT_CHECK int t;

     this() {
         n = 1;
         x = 2;
         t = 3;
     }
}


void Check(T)(T obj) {
     static if (is(T : Object)) {
         auto members = obj.tupleof;
         foreach (i, member; members) {
             how do i get all the members that are not marked by 
DO_NOT_CHECK ?
         }
     }
}

void main() {
     auto test = new Test();
     Check(test);
}
```
Hello, how do i get all the members that are not marked by 
DO_NOT_CHECK on Check() ?
Dec 01
next sibling parent Bradley Chatha <sealabjaster gmail.com> writes:
On Sunday, 1 December 2024 at 12:54:27 UTC, Joan wrote:
 Hello, how do i get all the members that are not marked by 
 DO_NOT_CHECK on Check() ?
The following should work: ```d void Check(T)(T obj) { import std.stdio : writeln; import std.traits : hasUDA; static if (is(T : Object)) { auto members = obj.tupleof; foreach (i, member; members) { static if(hasUDA!(obj.tupleof[i], DO_NOT_CHECK)) writeln("DO NOT CHECK: ", i); else writeln("CHECK: ", i); } } } ``` It may be worth giving `std.traits` a check over, since it has other goodies like https://dlang.org/phobos/std_traits.html#getSymbolsByUDA One oddity about D is that UDA information is _very_ easy to lose. You'd naturally think that you could do `hasUDA!(member, DO_NOT_CHECK)`, but unfortunately `member` no longer contains any information about its UDAs - it just becomes a normal `int`, so you have to directly index `obj.tupleof` in order to reference the original symbol. You can see this more clearly with the following function: ```d void Check(T)(T obj) { import std.stdio : writeln; import std.traits : hasUDA; static if (is(T : Object)) { auto members = obj.tupleof; foreach (i, member; members) { pragma(msg, __traits(getAttributes, member)); pragma(msg, __traits(getAttributes, obj.tupleof[i])); } } } ``` Which will print (I added the comments): ``` AliasSeq!() // Member n - using member AliasSeq!() // Member n - using obj.tupleof AliasSeq!() // Member x - using member AliasSeq!() // Member x - using obj.tupleof AliasSeq!() // Member t - using member AliasSeq!((DO_NOT_CHECK)) // Member t - using obj.tupleof ``` Hope that helps.
Dec 01
prev sibling parent Bradley Chatha <sealabjaster gmail.com> writes:
On Sunday, 1 December 2024 at 12:54:27 UTC, Joan wrote:
 ...
Just realised as well that this is in the General group. It'd be best to ask questions like this in the Learn group in the future :)
Dec 01