www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to check that a member function is generated by compiler?

reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
This code
```d
import std.sumtype: SumType;

struct A
{
     SumType!int b;
}

static foreach(sym; __traits(allMembers, A))
     pragma(msg,sym);
```
prints
```
b
opAssign
```

How can I check that `opAssign` is generated by compiler and 
doesn't exist in the original code?
Feb 24 2022
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/24/22 20:44, Andrey Zherikov wrote:

 How can I check that `opAssign` is generated by compiler and doesn't 
 exist in the original code?
I think this one: https://dlang.org/phobos/std_traits.html#hasElaborateAssign Ali
Feb 24 2022
parent reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
On Friday, 25 February 2022 at 05:25:14 UTC, Ali Çehreli wrote:
 On 2/24/22 20:44, Andrey Zherikov wrote:

 How can I check that `opAssign` is generated by compiler and 
 doesn't exist in the original code?
I think this one: https://dlang.org/phobos/std_traits.html#hasElaborateAssign Ali
This take a struct as an argument, not a member, so I need to do `if(sym == "opAssign")`. I gave this another thought and checking the symbol against `"opAssign"` would be good enough for my use case. Another interesting observation - is there any explanation why `typeof` returns different results for generated `opAssign`? ```d import std.sumtype: SumType; struct A { SumType!int b; } static foreach(sym; __traits(allMembers, A)) { // ref A(A p) return,opAssign pragma(msg, typeof(__traits(getMember, A, sym)).stringof,",",sym); // true,pure nothrow nogc ref safe A(A p) return,opAssign pragma(msg, is(typeof(__traits(getMember, A, sym)) == function),",", typeof(__traits(getMember, A, sym)).stringof,",",sym); // pure nothrow nogc ref safe A(A p) return,opAssign pragma(msg, typeof(__traits(getMember, A, sym)).stringof,",",sym); } ``` If I move `foreach` loop into a function (e.g. `main`) then the first pragma prints the same as the others.
Feb 25 2022
parent reply Paul Backus <snarwin gmail.com> writes:
On Friday, 25 February 2022 at 14:25:22 UTC, Andrey Zherikov 
wrote:
 Another interesting observation - is there any explanation why 
 `typeof` returns different results for generated `opAssign`?
[...]
 If I move `foreach` loop into a function (e.g. `main`) then the 
 first pragma prints the same as the others.
It sounds like this is probably an inconsistency in `.stringof`, not `typeof`. I couldn't say what causes it, but it's not the first time I've seen `.stringof` give inconsistent results. (E.g., https://issues.dlang.org/show_bug.cgi?id=18269)
Feb 25 2022
next sibling parent Andrey Zherikov <andrey.zherikov gmail.com> writes:
On Friday, 25 February 2022 at 16:24:46 UTC, Paul Backus wrote:
 On Friday, 25 February 2022 at 14:25:22 UTC, Andrey Zherikov 
 wrote:
 Another interesting observation - is there any explanation why 
 `typeof` returns different results for generated `opAssign`?
[...]
 If I move `foreach` loop into a function (e.g. `main`) then 
 the first pragma prints the same as the others.
It sounds like this is probably an inconsistency in `.stringof`, not `typeof`. I couldn't say what causes it, but it's not the first time I've seen `.stringof` give inconsistent results. (E.g., https://issues.dlang.org/show_bug.cgi?id=18269)
I see. Thanks
Feb 25 2022
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/25/22 08:24, Paul Backus wrote:

 I've seen `.stringof` give inconsistent results. (E.g.,
 https://issues.dlang.org/show_bug.cgi?id=18269)
That must be a part of the reasons why Adam D Ruppe repeats that .stringof should not be used for such programmatic purposes e.g. if I remember correctly should not be used in string mixins. Ali
Feb 25 2022
parent Paul Backus <snarwin gmail.com> writes:
On Friday, 25 February 2022 at 19:06:25 UTC, Ali Çehreli wrote:
 On 2/25/22 08:24, Paul Backus wrote:

 I've seen `.stringof` give inconsistent results. (E.g.,
 https://issues.dlang.org/show_bug.cgi?id=18269)
That must be a part of the reasons why Adam D Ruppe repeats that .stringof should not be used for such programmatic purposes e.g. if I remember correctly should not be used in string mixins.
There's also the fact that the language spec makes no guarantees about the result of .stringof, so it is free to change at any time in ways that may break your code.
Feb 25 2022