[Issue 4476] New: __traits for more kinds of names

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jul 16 16:44:37 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4476

           Summary: __traits for more kinds of names
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-07-16 16:44:36 PDT ---
This is relative to page 22 of The D Programming Language.

This piece of D2 code shown in the book instantiates a class defined inside the
current module named stats:
Object.factory("stats." ~ arg);

Generally in code it's better to apply the DRY principle, avoiding to duplicate
information that later can get out of sync (in this case a change in the module
name breaks the code inside the module).

This currently works (dmd v2.047), but it's not nice, and performs run-time
computations for something that is known statically (maybe there are already
better ways to do it):

Object.factory(split(to!string({class C {}; return new C;}()), ".")[0] ~
".Foo");

The problem can be solved with few traits, able to tell at compile-time:
__traits(thisModuleName) : the name of the current module.
__traits(thisStructName) : the name of the current struct (where this line is
contained), useful to create its toString that shows the struct name too.
__traits(thisClassName) : the name of the current class (this can be found with
this.classinfo.name, but there is no need to compute it at runtime).
__traits(thisFunctionName) : the name of the current function, useful for CTFE
& string mixins.

Possibly there are already ways to find such strings at compile-time, but
having a simple standard way to do it is good (better than having magic
variables like __function_name__). Some more power of static introspection can
be very useful in D.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list