Alias std.typecons.WhiteHole
WhiteHole!Base
is a subclass of Base
which automatically implements
all abstract member functions as functions that always fail. These functions
simply throw an Error
and never return. Whitehole
is useful for
trapping the use of class member functions that haven't been implemented.
The name came from Class::WhiteHole Perl module by Michael G Schwern.
Parameters
Name | Description |
---|---|
Base | A non-final class for WhiteHole to inherit from. |
See Also
Example
import std .exception : assertThrown;
static class C
{
abstract void notYetImplemented();
}
auto c = new WhiteHole!C;
assertThrown!NotImplementedError(c .notYetImplemented()); // throws an Error
Example
import std .exception : assertThrown;
// nothrow
{
interface I_1
{
void foo();
void bar() nothrow;
}
auto o = new WhiteHole!I_1;
assertThrown!NotImplementedError(o .foo());
assertThrown!NotImplementedError(o .bar());
}
// doc example
{
static class C
{
abstract void notYetImplemented();
}
auto c = new WhiteHole!C;
try
{
c .notYetImplemented();
assert(0);
}
catch (Error e) {}
}
}
/**
`AutoImplement` automatically implements (by default) all abstract member
functions in the class or interface `Base` in specified way.
The second version of `AutoImplement` automatically implements
`Interface`, while deriving from `BaseClass`.
Params:
how = template which specifies _how functions will be implemented/overridden.
Two arguments are passed to `how`: the type `Base` and an alias
to an implemented function. Then `how` must return an implemented
function body as a string.
The generated function body can use these keywords:
<ul> <li>`a0`, `a1`, …: arguments passed to the function;</li>
<li>`args`: a tuple of the arguments;</li>
<li>`self`: an alias to the function itself;</li>
<li>`parent`: an alias to the overridden function (if any).</li>
</ul>
You may want to use templated property functions (instead of Implicit
Template Properties) to generate complex functions:
// Prints log messages for each call to overridden functions. string generateLogger(C, alias fun)() @property { import std.traits; enum qname = C.stringof ~ "." ~ _traits(identifier, fun); string stmt;
stmt ~= q{ struct Importer { import std.stdio; } };
stmt ~= Importer
~ qname ~ (", args, ")
;
static if (!_traits(isAbstractFunction, fun))
{
static if (is(ReturnType!fun == void))
stmt ~= q{ parent(args); };
else
stmt ~= q{
auto r = parent(args);
Importer.writeln("--> ", r);
return r;
};
}
return stmt;
Authors
Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara