[Question] About mixin, template and alias
Michael
pr at m1xa.com
Sun Sep 22 11:31:18 PDT 2013
/////// fire.d
import std.stdio;
alias void delegate() EventHandler;
class Event(T)
{
private T[] _events;
public void opOpAssign(string op)(T param) if (op == "~")
{
writeln(param.funcptr);
_events ~= param;
}
public void opCall(ParamType ...)(ParamType params)
{
emit(params);
}
protected void emit(ParamType ...)(ParamType params)
{
foreach (event; _events)
event(params);
}
}
mixin template AddEvent(DelegateType, string member)
{
auto eventObserver = new Event!DelegateType();
mixin("alias eventObserver " ~ member ~ ";");
}
class Fire
{
public mixin AddEvent!(EventHandler, "click");
}
/////// water.d
import std.stdio;
import fire;
class Water : Fire
{
}
void main()
{
auto fire = new Fire();
auto water = new Water();
water.click ~= () { writeln("Water!"); };
fire.click ~= () { writeln("Fire!"); };
fire.click();
}
Output:
Water!
Fire!
Someone can explain me a behaviour of above code? Why not "Fire!"
only?
More information about the Digitalmars-d-learn
mailing list