www.digitalmars.com         C & C++   DMDScript  

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

reply d-bugmail puremagic.com writes:
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 puremagic.com
        ReportedBy: ttanjo gmail.com



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: -------
Jul 08 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10570




Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/bb50a33f1b27d94b7558661df1719d6e72afecda
Fixes Issue 10570

https://github.com/D-Programming-Language/phobos/commit/3095e8904a46a849a62ec03ea48f85525c7fe95a


Fix Issue 10570 - Example of `how` function for AutoImplement should work for
non-abstract class

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 15 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10570


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 15 2013