Jump to page: 1 2 3
Thread overview
core.reflect vs c++ reflection
Sep 26, 2021
Stefan Koch
Sep 26, 2021
Stefan Koch
Sep 27, 2021
bauss
Sep 27, 2021
Stefan Koch
Sep 27, 2021
bauss
Sep 27, 2021
Stefan Koch
Sep 27, 2021
WebFreak001
Sep 27, 2021
Stefan Koch
Sep 27, 2021
russhy
Sep 28, 2021
Stefan Koch
Sep 28, 2021
WebFreak001
Sep 28, 2021
Stefan Koch
Sep 28, 2021
bauss
Sep 28, 2021
Adam D Ruppe
Sep 28, 2021
Per Nordlöw
Sep 28, 2021
Alexandru Ermicioi
Sep 28, 2021
Per Nordlöw
Sep 28, 2021
Adam D Ruppe
Sep 28, 2021
Per Nordlöw
Sep 28, 2021
Adam D Ruppe
Sep 28, 2021
bauss
Sep 28, 2021
Adam D Ruppe
Sep 28, 2021
bauss
September 26, 2021

Good day,

It's hard to find good examples of the c++ reflection ts so I've copied one I've found in an earlier proposal
I am just putting the proposed c++ syntax here, next to how my core.reflect library would handle this case.

#include <meta>
template<Enum T>
std::string to_string(T value) { // Could also be marked constexpr
    for constexpr (auto e : std::meta::members_of(reflexpr(T)) {
        if (exprid(e) == value) {
            return std::meta::name_of(e);
        }
    }
    return "<unnamed>";
}
import core.reflect.reflect;
string toString(E)(E enumValue)
{
    static immutable ed = cast(EnumDeclaration)nodeFromName(E);
    foreach(m; ed.members)
    {
        if ((cast(IntegerLiteral)m.value).value == enumValue)
        {
            return m.name;
        }
    }
    return "unnamed";
}

Please let know what you think.

September 26, 2021

On Sunday, 26 September 2021 at 19:03:24 UTC, Stefan Koch wrote:

>

Good day,

>

Please let know what you think.

Ugh typo.

>

static immutable ed = cast(EnumDeclaration)nodeFromName(E);

is supposed to be:

static immutable ed = cast(EnumDeclaration)nodeFromName("E");

September 27, 2021

On Sunday, 26 September 2021 at 19:09:31 UTC, Stefan Koch wrote:

>

On Sunday, 26 September 2021 at 19:03:24 UTC, Stefan Koch wrote:

>

Good day,

>

Please let know what you think.

Ugh typo.

>

static immutable ed = cast(EnumDeclaration)nodeFromName(E);

is supposed to be:

static immutable ed = cast(EnumDeclaration)nodeFromName("E");

What about E.stringof? Instead of hardcoding names :) Not sure if stringof will return E in all cases, so obviously only if it works.

September 27, 2021

On Monday, 27 September 2021 at 06:25:07 UTC, bauss wrote:

>

On Sunday, 26 September 2021 at 19:09:31 UTC, Stefan Koch wrote:

>

On Sunday, 26 September 2021 at 19:03:24 UTC, Stefan Koch wrote:

>

Good day,

>

Please let know what you think.

Ugh typo.

>

static immutable ed = cast(EnumDeclaration)nodeFromName(E);

is supposed to be:

static immutable ed = cast(EnumDeclaration)nodeFromName("E");

What about E.stringof? Instead of hardcoding names :) Not sure if stringof will return E in all cases, so obviously only if it works.

E stringof would return the name of the type;
In simple cases and garbage in others.

Whereas the name E always refers to the type used in the toString template.
Also "E" is less typing than E.stringof

September 27, 2021

On Monday, 27 September 2021 at 07:28:46 UTC, Stefan Koch wrote:

>

On Monday, 27 September 2021 at 06:25:07 UTC, bauss wrote:

>

On Sunday, 26 September 2021 at 19:09:31 UTC, Stefan Koch wrote:

>

On Sunday, 26 September 2021 at 19:03:24 UTC, Stefan Koch wrote:

>

Good day,

>

Please let know what you think.

Ugh typo.

>

static immutable ed = cast(EnumDeclaration)nodeFromName(E);

is supposed to be:

static immutable ed = cast(EnumDeclaration)nodeFromName("E");

What about E.stringof? Instead of hardcoding names :) Not sure if stringof will return E in all cases, so obviously only if it works.

E stringof would return the name of the type;
In simple cases and garbage in others.

Whereas the name E always refers to the type used in the toString template.
Also "E" is less typing than E.stringof

Yeah I figured it wouldn't work that easily.

In this case yeah "E" wouldn't be a problem but if it was "ThisTypeName" you might end up with a typo.

September 27, 2021

On Monday, 27 September 2021 at 07:32:48 UTC, bauss wrote:

>

Yeah I figured it wouldn't work that easily.

In this case yeah "E" wouldn't be a problem but if it was "ThisTypeName" you might end up with a typo.

Yes, but you can typo ThisTypeName whether you type it with quotes around it or without.
It's still an identifier. Which may not be Identifying anything if it's mistyped.
The reason why I am using a string is that I wanted to avoid having to introduce a special expression. see c++ reflexpr

September 27, 2021

On Monday, 27 September 2021 at 08:39:45 UTC, Stefan Koch wrote:

>

On Monday, 27 September 2021 at 07:32:48 UTC, bauss wrote:

>

Yeah I figured it wouldn't work that easily.

In this case yeah "E" wouldn't be a problem but if it was "ThisTypeName" you might end up with a typo.

Yes, but you can typo ThisTypeName whether you type it with quotes around it or without.
It's still an identifier. Which may not be Identifying anything if it's mistyped.
The reason why I am using a string is that I wanted to avoid having to introduce a special expression. see c++ reflexpr

If it's mistyped, the compiler will complain that it doesn't exist

When it's an identifier, IDE refactorings (automated renaming) will find it, while it wouldn't find it in a string, especially the more generic the name is (which is common for template types T)

C# has nameof(T) which just returns "T" or for nameof(Foo.T) it also just returns "T"

September 27, 2021

On Monday, 27 September 2021 at 13:12:32 UTC, WebFreak001 wrote:

>

If it's mistyped, the compiler will complain that it doesn't exist

core.reflect will tell you that you tried to resolve a name which does not exist as well.

>

When it's an identifier, IDE refactorings (automated renaming) will find it, while it wouldn't find it in a string, especially the more generic the name is (which is common for template types T)

C# has nameof(T) which just returns "T" or for nameof(Foo.T) it also just returns "T"

That's valid.
I have to think about what to do about that.
Maybe remove the "" for the refactoring?
a simple search for nodeFromName\(\"$ID\"\) maybe?
It's the only primitive in code reflect which does this.

September 27, 2021

using traits:

string enum_to_str(E)(E v) if (is(E == enum))
{
    final switch (v) with(E)
    {
        static foreach (m; __traits(allMembers, E))
        {
    case mixin(m):
            return m;
        }
    }
}

core.reflect is nicer than traits because you actually get to use actual D code

But the casting is kinda ugly, and it gives me java nightmares.. so the C++ actually feels much better

September 28, 2021

On Monday, 27 September 2021 at 13:12:32 UTC, WebFreak001 wrote:

>

On Monday, 27 September 2021 at 08:39:45 UTC, Stefan Koch wrote:

>

On Monday, 27 September 2021 at 07:32:48 UTC, bauss wrote:

>

Yeah I figured it wouldn't work that easily.

In this case yeah "E" wouldn't be a problem but if it was "ThisTypeName" you might end up with a typo.

Yes, but you can typo ThisTypeName whether you type it with quotes around it or without.
It's still an identifier. Which may not be Identifying anything if it's mistyped.
The reason why I am using a string is that I wanted to avoid having to introduce a special expression. see c++ reflexpr

If it's mistyped, the compiler will complain that it doesn't exist

When it's an identifier, IDE refactorings (automated renaming) will find it, while it wouldn't find it in a string, especially the more generic the name is (which is common for template types T)

C# has nameof(T) which just returns "T" or for nameof(Foo.T) it also just returns "T"

Yeah, I really wish we had an alternative to stringof like nameof. I often forgot that aliases don't work well with it, as displayed here :)

« First   ‹ Prev
1 2 3