import std.stdio;
class Collection( Item )
{
void add( Item item ) { }
void addRange( Item[] items )
{
foreach( item ; items ) add( item );
}
void say( Message )( Message message )
{
writeln( "Collection says: " , message );
}
}
class CollectionWithCounter( Item , CollectionInner = Collection!Item )
{
private CollectionInner inner;
alias inner this;
private int count;
this( CollectionInner inner = new CollectionInner )
{
this.inner = inner;
}
void add( Item item )
{
inner.add( item );
count += 1;
}
void addRange( Item[] items )
{
inner.addRange( items );
count += items.length;
}
}
void main()
{
auto foo = new CollectionWithCounter!int;
foo.add(1);
foo.add(2);
foo.add(3);
foo.addRange([ 1 , 2 , 3 ]);
foo.say( foo.count );
}
aW1wb3J0IHN0ZC5zdGRpbzsKCmNsYXNzIENvbGxlY3Rpb24oIEl0ZW0gKQp7CiAgICB2b2lkIGFkZCggSXRlbSBpdGVtICkgeyB9Cgl2b2lkIGFkZFJhbmdlKCBJdGVtW10gaXRlbXMgKQoJewoJCWZvcmVhY2goIGl0ZW0gOyBpdGVtcyApIGFkZCggaXRlbSApOwoJfQoJdm9pZCBzYXkoIE1lc3NhZ2UgKSggTWVzc2FnZSBtZXNzYWdlICkKCXsKCQl3cml0ZWxuKCAiQ29sbGVjdGlvbiBzYXlzOiAiICwgbWVzc2FnZSApOwoJfQp9CgpjbGFzcyBDb2xsZWN0aW9uV2l0aENvdW50ZXIoIEl0ZW0gLCBDb2xsZWN0aW9uSW5uZXIgPSBDb2xsZWN0aW9uIUl0ZW0gKQp7Cglwcml2YXRlIENvbGxlY3Rpb25Jbm5lciBpbm5lcjsKCWFsaWFzIGlubmVyIHRoaXM7CgoJcHJpdmF0ZSBpbnQgY291bnQ7CgogICAgdGhpcyggQ29sbGVjdGlvbklubmVyIGlubmVyID0gbmV3IENvbGxlY3Rpb25Jbm5lciApCgl7CgkJdGhpcy5pbm5lciA9IGlubmVyOwoJfQoKCXZvaWQgYWRkKCBJdGVtIGl0ZW0gKQoJewoJCWlubmVyLmFkZCggaXRlbSApOwoJCWNvdW50ICs9IDE7Cgl9Cg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