On Saturday, 26 December 2020 at 12:38:21 UTC, sighoya wrote:
On Friday, 25 December 2020 at 23:04:15 UTC, Ali Çehreli wrote:

I am probably misunderstanding it but there is the .stringof property for all types: T.stringof.

But does stringof really print the declaration as string and not the type name itself?

I found: https://dlang.org/spec/property.html#stringof

I am probably stating the obvious but you normally import the interesting modules of that library anyway and the type information is available that way.

Ali

Well, certainly, yes. But only if `stringof` would do the job. The best thing would be to retrieve whole modules (abstract files) as string. On top of that we can retrieve any value/type declaration.

with __traits(allMembers) you can rebuild the declaration. For a struct that works well.

---
struct E {int a;}
struct S
{
    ulong[] a;
    @E(0) const int b;
    void v() const {}
    void v(int) const {}
}

string getDeclaration(T)()
if (is(T == struct))
{
    import std.traits, std.meta;
    string result = "struct " ~ T.stringof ~ " {\n";
    static foreach (m; __traits(allMembers, T))
    {{
        static if (isCallable!(__traits(getMember, T, m)))
            alias sym = __traits(getOverloads, T, m);
        else
            alias sym = AliasSeq!(__traits(getMember, T, m));
        static foreach (s; sym)
        {
            result ~= "    ";
            static foreach (a; __traits(getAttributes, s))
                result ~= "@" ~ a.stringof ~ " ";
            static if (is(typeof(s)))
                result ~= typeof(s).stringof ~ " " ~ m ~ ";\n";
        }
    }}
    result ~= "}";
    return result;
}

pragma(msg, getDeclaration!S);
---

Reply via email to