Type functions example printing class hierarchy
Stefan Koch
uplink.coder at googlemail.com
Fri Aug 21 18:03:06 UTC 2020
On Friday, 21 August 2020 at 16:19:36 UTC, Stefan Koch wrote:
> Hi there,
>
> In a previous thread I talked about how pattern-matching is
> expressions are a pain to implement for type functions.
> So I've taken the liberty to add an additional __trait to get
> the super type
> Below is an example of how it might be used to visualize a
> class hierarchy
> ---
> class A {}
> class B : A {}
> class C : A {}
>
> string class_hierarchy(alias T)
> {
> string result;
>
> alias super_type;
> // for now this is a typefunction-only __trait
> super_type = __traits(getSuperType, T);
> while(is(super_type))
> {
> result ~= T.stringof ~ " -> " ~ super_type.stringof ~
> "\n";
> T = super_type;
> super_type = __traits(getSuperType, T);
> }
>
> return result;
> }
>
> pragma(msg, "class_hierarchy(A): \n", class_hierarchy(A));
> pragma(msg, "class_hierarchy(B): \n", class_hierarchy(B));
> pragma(msg, "class_hierarchy(C): \n", class_hierarchy(C));
> ---
>
> Of which the output is:
>
> class_hierarchy(A):
> A -> Object
>
> class_hierarchy(B):
> B -> A
> A -> Object
>
> class_hierarchy(C):
> C -> A
> A -> Object
>
> If there any questions about how this works please fire away.
>
> Cheers, Stefan
>
> PS. the addition of __traits(getSuperType) added 90 lines of
> code.
> I think that's a worthwhile treat-off
Here's the recursive template:
template type_h_template(T)
{
static if (is(T S == super) && S.length)
{
enum type_h_template = ( T.stringof ~ " -> " ~
S[0].stringof ~ "\n" ~ .type_h_template!(S) );
}
else
{
enum type_h_template = "";
}
}
Which looks less jarring than I first thought.
More information about the Digitalmars-d
mailing list