View source code
Display the source code in std/typecons.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Class std.typecons.AutoImplement
automatically implements (by default) all abstract member
functions in the class or interface AutoImplement
Base
in specified way.
Inherits from
-
(base class)Object
Methods
Name | Description |
---|---|
factory
|
Create instance of class specified by the fully qualified name
classname .
The class must either have no constructors or have
a default constructor.
|
opCmp
|
Compare with another Object obj.
|
opEquals
|
Returns !=0 if this object does have the same contents as obj.
|
toHash
|
Compute hash function for Object .
|
toString
|
Convert Object to a human readable string.
|
Parameters
Name | Description |
---|---|
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:
std.traits ;
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(ReturnType!fun == void))
stmt ~= q{ parent(args); };
else
stmt ~= q{
auto r = parent(args);
Importer.writeln("--> ", r);
return r;
};
}
return stmt;
}
--------------------
|
what | template which determines what functions should be
implemented/overridden.
An argument is passed to what : an alias to a non-final member
function in Base . Then what must return a boolean value.
Return true to indicate that the passed function should be
implemented/overridden.
--------------------
// Sees if fun returns something.
enum bool hasValue(alias fun) = !is(ReturnType!(fun) == void);
--------------------
|
Note
Generated code is inserted in the scope of
module. Thus,
any useful functions outside std.typecons
cannot be used in the generated
code. To workaround this problem, you may std.typecons
import
necessary things in a
local struct, as done in the generateLogger()
template in the above
example.
BUGS
- Variadic arguments to constructors are not forwarded to super.
- Deep interface inheritance causes compile error with messages like
"Error: function std.typecons.AutoImplement!(Foo).AutoImplement.bar does not override any function". [Bugzilla 2525, Bugzilla 3525]
- The
parent
keyword is actually a delegate to the super class' corresponding member function. [Bugzilla 2540] - Using alias template parameter in
how
and/orwhat
may cause strange compile error. Use templatetuple
parameter instead to workaround this problem. [Bugzilla 4217]
Authors
Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara