I'm currently developing a pretty printing system in D with backend support for text, html, mathml, latex, etc.

To make it loosely decoupled in compile-time if currently make use __traits in the following way.

    static if (__traits(hasMember, arg, "toHTML"))
    {
        if (viz.form == VizForm.HTML)
        {
            return viz.ppRaw(arg.toHTML);
        }
    }
else static if (__traits(hasMember, arg, "toMathML")) // TODO: Change to __traits(compiles, auto x = arg.toMathML()) or *callable*
    {
        if (viz.form == VizForm.HTML)
        {
            // TODO: Check for MathML support on backend
            return viz.ppRaw(arg.toMathML);
        }
    }
    else static if (__traits(hasMember, arg, "toLaTeX"))
    {
        if (viz.form == VizForm.LaTeX)
        {
            return viz.ppRaw(arg.toLaTeX);
        }
    }

A more flexible solution is to not require toX to be a member function of type specific (system) types to printed.

What is the preffered (fast) way to check at compile-time if an instance x of a type T can be used *either* as

    f(x)

or

    x.f?

Reply via email to