Taming templates and mixin magic: type inpector helper in D/Phobos?

Uknown sireeshkodali1 at gmail.com
Wed Apr 4 14:35:18 UTC 2018


On Wednesday, 4 April 2018 at 05:28:57 UTC, Carlos Navarro wrote:
> As a newbie in D (and making a lots of mistakes), I've found 
> myself relying heavily in the use of a rudimentary type 
> inspector to visualize my templated code instantiations.
> It's simple and incomplete as hell but good enough to see what 
> happens under the hood quickly.
>
> QUESTION: Is there a function like that already in D?
>
>
> //real-life example
> goodEnoughTypeInspector!Human;
> goodEnoughTypeInspector!(Database!Human);
>
> //compile-time output
> Inspecting type: Human
>          - public string name
>          - public string address
>          - public bool active
>
> Inspecting type: Database!(Human)
>          - private string[] name
>          - private string[] address
>          - private bool[] active
>          - public pure nothrow @nogc @safe ulong() count
>          - public void insert

you might want to check std.traits [0] and __traits [1]. They 
both contain functions that may be useful. To get members you 
could use
__traits(allMembers, MyTemplate!SomeType);

Just check through the docs for relevant functions. You can then 
compose the relevant function. I wrote a basic version you can 
play with [2].


[0]: https://dlang.org/phobos/std_traits.html
[1]: https://dlang.org/spec/traits.html
[2]: https://run.dlang.io/is/COZ89T


More information about the Digitalmars-d-learn mailing list