Thread overview
How to check that a member function is generated by compiler?
Feb 25, 2022
Andrey Zherikov
Feb 25, 2022
Ali Çehreli
Feb 25, 2022
Andrey Zherikov
Feb 25, 2022
Paul Backus
Feb 25, 2022
Andrey Zherikov
Feb 25, 2022
Ali Çehreli
Feb 25, 2022
Paul Backus
February 25, 2022

This code

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?

February 24, 2022
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
February 25, 2022

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?

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.

February 25, 2022

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)

February 25, 2022

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

February 25, 2022
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

February 25, 2022
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.