Jump to page: 1 2
Thread overview
core.reflect vs. __traits
Jul 03, 2021
Stefan Koch
Jul 03, 2021
zjh
Jul 03, 2021
Stefan Koch
Jul 03, 2021
Stefan Koch
Jul 03, 2021
Stefan Koch
Jul 03, 2021
Bruce Carneal
Jul 03, 2021
Alexandru Ermicioi
Jul 03, 2021
Stefan Koch
Jul 04, 2021
Alexandru Ermicioi
July 03, 2021

Good Day D Community,

I just did a little test run of using core.reflect on the enum TOK within DMD.
(Which was successful yay. ;))

Then I compared it against the __traits version.

import dmd.tokens;
version(UseCoreReflect)
{
  import core.reflect.reflect;
  static immutable e = cast(immutable EnumDeclaration) nodeFromName("TOK");

  pragma(msg,
    () {
      import std.conv;
      string result;

      result ~= "enum " ~ e.name ~ " {\n";

      foreach(m;e.members)
      {
          IntegerLiteral l = cast(IntegerLiteral) m.value;
          result ~= "  " ~ m.name ~ " = " ~ to!string(l.value) ~ ",\n";
      }

      result ~= "}";
      return result;
    } ()
  );

}

version(UseTraits)
{
  enum members = __traits(allMembers, TOK);

  pragma(msg,
    () {
      import std.conv;
      string result;

      result ~= "enum " ~ TOK.stringof ~ "{\n";

      static foreach(m;members)
      {
          result ~= "  " ~ m ~ " = " ~ to!string(cast(int)mixin("TOK." ~ m)) ~ ",\n";
      }

      result ~= "}";
      return result;
    } ()
  );
}

In terms of code I do prefer the core.traits version because that one does not use a mixin and doesn't force you to do a static foreach also it can trivially be factored into a runtime function which doesn't stress CTFE out. Whereas the __traits version cannot trivially be factored to do most of the work at runtime.
I guess you could build a string[] with member names and index that runtime but it's not as trivial as just calling the function at runtime with an EnumDeclaration object generated at compile time.

Please let me know what you think.

P.S. In terms of performance the core.reflect version is faster by 2% on average which doesn't matter in the slightest using such tiny test-cases.

July 03, 2021

On Saturday, 3 July 2021 at 11:59:03 UTC, Stefan Koch wrote:

>

Good Day D Community,

When can we used core.reflect?

July 03, 2021

On Saturday, 3 July 2021 at 12:44:00 UTC, zjh wrote:

>

On Saturday, 3 July 2021 at 11:59:03 UTC, Stefan Koch wrote:

>

Good Day D Community,

When can we used core.reflect?

I am not sure ;)

At this point it's a prototype which I have been developing for less than 2 weeks.
As soon it works consistently performant on large code-bases.

The last thing D needs is another toy feature that breaks down under stress and pressure.

July 03, 2021

On 7/3/21 7:59 AM, Stefan Koch wrote:

>

          result ~= "  " ~ m ~ " = " ~ to!string(cast(int)mixin("TOK." ~ m)) ~ ",\n";

"because [the __traits version uses] a mixin"

What about:

result ~= " " ~ m ~ " = " ~ to!string(int(__traits(getMember, TOK, m))) ~ ",\n";

-Steve

July 03, 2021

On Saturday, 3 July 2021 at 13:09:26 UTC, Steven Schveighoffer wrote:

>

On 7/3/21 7:59 AM, Stefan Koch wrote:

>

          result ~= "  " ~ m ~ " = " ~ to!string(cast(int)mixin("TOK." ~ m)) ~ ",\n";

"because [the __traits version uses] a mixin"

What about:

result ~= " " ~ m ~ " = " ~ to!string(int(__traits(getMember, TOK, m))) ~ ",\n";

-Steve

That needs TOK as a template type parameter as well. and therefore forces you to extract the strings at compile time into an auxiliary data-structure. (most likely string[])

July 03, 2021

On 7/3/21 9:15 AM, Stefan Koch wrote:

>

On Saturday, 3 July 2021 at 13:09:26 UTC, Steven Schveighoffer wrote:

>

On 7/3/21 7:59 AM, Stefan Koch wrote:

>

           result ~= "  " ~ m ~ " = " ~ to!string(cast(int)mixin("TOK." ~ m)) ~ ",\n";

"because [the __traits version uses] a mixin"

What about:

result ~= " " ~ m ~ " = " ~ to!string(int(__traits(getMember, TOK, m))) ~ ",\n";

That needs TOK as a template type parameter as well. and therefore forces you to extract the strings at compile time into an auxiliary data-structure. (most likely string[])

My point is, if mixin("TOK." ~ m) works, then __traits(getMember, TOK, m) should also, and therefore no mixin required.

-Steve

July 03, 2021

On Saturday, 3 July 2021 at 11:59:03 UTC, Stefan Koch wrote:

>

[big snip]

Please let me know what you think.

Nice! The eminently readable structs/API displace the current all-in-the-programmers-head meta programming "type" systems (various collections of strings, conventions, yet-more-layering, alls-you-gotta-do-is-recur hacks, if-you-were-a-real-programmer-you'd-find-this-trivial hacks, ...).

With changes like these meta programming starts to look less special, less heroic. That's a good look.

Good luck in your further explorations.

July 03, 2021

On Saturday, 3 July 2021 at 15:09:38 UTC, Steven Schveighoffer wrote:

>

On 7/3/21 9:15 AM, Stefan Koch wrote:

>

On Saturday, 3 July 2021 at 13:09:26 UTC, Steven Schveighoffer wrote:

>

On 7/3/21 7:59 AM, Stefan Koch wrote:

>

           result ~= "  " ~ m ~ " = " ~ to!string(cast(int)mixin("TOK." ~ m)) ~ ",\n";

"because [the __traits version uses] a mixin"

What about:

result ~= " " ~ m ~ " = " ~ to!string(int(__traits(getMember, TOK, m))) ~ ",\n";

That needs TOK as a template type parameter as well. and therefore forces you to extract the strings at compile time into an auxiliary data-structure. (most likely string[])

My point is, if mixin("TOK." ~ m) works, then __traits(getMember, TOK, m) should also, and therefore no mixin required.

-Steve

Fair point.
And getMember does look somewhat nicer than the string mixin as well ;)

July 03, 2021

On Saturday, 3 July 2021 at 11:59:03 UTC, Stefan Koch wrote:

>

Good Day D Community,
...
Please let me know what you think.

Hi, any thoughts, on integrating core.reflect with typeinfo classes?
Seems like, right place for reflection information to be stored.

Also any plans on providing a good interface for invoking functions and class members?
Would be nice to have two sets of functionality, one for compile time, and one at run time.

Best regards,
Alexandru.

July 03, 2021

On Saturday, 3 July 2021 at 15:46:07 UTC, Alexandru Ermicioi wrote:

>

On Saturday, 3 July 2021 at 11:59:03 UTC, Stefan Koch wrote:

>

Good Day D Community,
...
Please let me know what you think.

Hi, any thoughts, on integrating core.reflect with typeinfo classes?
Seems like, right place for reflection information to be stored.

Also any plans on providing a good interface for invoking functions and class members?
Would be nice to have two sets of functionality, one for compile time, and one at run time.

Best regards,
Alexandru.

typeinfo is a failed experiment in my opinion which has never been useful and this is why I am working on core.reflect.
The functionality for runtime and for compile time (as far as working with reflection classes in concerned) should be the same.
Of course, the to be unveiled core.codegen cannot work at runtime.

« First   ‹ Prev
1 2