[Issue 10570] New: Example of `how` function for AutoImplement should work for non-abstract class

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jul 8 04:52:48 PDT 2013


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

           Summary: Example of `how` function for AutoImplement should
                    work for non-abstract class
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: ttanjo at gmail.com


--- Comment #0 from Tomoya Tanjo <ttanjo at gmail.com> 2013-07-08 04:52:47 PDT ---
The generateLogger function is an example of `how` function for
std.typecons.AutoImplement and
is defined in the comment of AutoImplement.

It works for interface and abstract class, but it does not work for the
non-abstract class with a method 
that returns non-string (e.g. void).
The reason is that it only consider the return type of itself, not the return
type of method to be overridden.

The following code reproduce the problem. It should be compiled succesfully but
it does not:

----
// It is coped from the comment of std.typecons.AutoImplement.
// Prints log messages for each call to overridden functions.
string generateLogger(C, alias fun)() @property
{
    enum qname = C.stringof ~ "." ~ __traits(identifier, fun);
    string stmt;

    stmt ~= q{ struct Importer { import std.stdio; } };
    stmt ~= `Importer.writeln("Log: ` ~ qname ~ `(", args, ")");`;
    static if (!__traits(isAbstractFunction, fun))
    {
        static if (is(typeof(return) == void)) // typeof(return) is always
string!
            stmt ~= q{ parent(args); };
        else
            stmt ~= q{
                auto r = parent(args);
                Importer.writeln("--> ", r);
                return r;
            };
    }
    return stmt;
}

// A class to be overridden
class Foo{
    void bar(int a) { }
}

// Logger template
template Logger(Base)
{
    import std.typecons;
    alias Logger = AutoImplement!(Base, generateLogger, isThrowable);
}

// to avoid overriding toHash (it is nothrow but writeln is not nothrow)
template isThrowable(alias fun)
{
    import std.traits;
    enum isThrowable = !functionAttributes!fun ||
                       (functionAttributes!fun & !FunctionAttribute.nothrow_);
}

void main()
{
    auto foo = new Logger!Foo();
    foo.bar(13);
}
----

-- 
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