Bug 10570 – Example of `how` function for AutoImplement should work for non-abstract class
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-08T04:52:00Z
Last change time
2013-07-15T05:06:03Z
Keywords
pull
Assigned to
nobody
Creator
ttanjo
Comments
Comment #0 by ttanjo — 2013-07-08T04:52:47Z
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);
}
----
Comment #1 by github-bugzilla — 2013-07-15T05:05:01Z