fork download
  1. import std.stdio;
  2.  
  3. class Collection( Item )
  4. {
  5. void add( Item item ) { }
  6. void addRange( Item[] items )
  7. {
  8. foreach( item ; items ) add( item );
  9. }
  10. void say( Message )( Message message )
  11. {
  12. writeln( "Collection says: " , message );
  13. }
  14. }
  15.  
  16. class CollectionWithCounter( Item , CollectionInner = Collection!Item )
  17. {
  18. private CollectionInner inner;
  19. alias inner this;
  20.  
  21. private int count;
  22.  
  23. this( CollectionInner inner = new CollectionInner )
  24. {
  25. this.inner = inner;
  26. }
  27.  
  28. void add( Item item )
  29. {
  30. inner.add( item );
  31. count += 1;
  32. }
  33.  
  34. void addRange( Item[] items )
  35. {
  36. inner.addRange( items );
  37. count += items.length;
  38. }
  39. }
  40.  
  41. void main()
  42. {
  43. auto foo = new CollectionWithCounter!int;
  44.  
  45. foo.add(1);
  46. foo.add(2);
  47. foo.add(3);
  48.  
  49. foo.addRange([ 1 , 2 , 3 ]);
  50.  
  51. foo.say( foo.count );
  52. }
  53.  
Success #stdin #stdout 0.01s 13944KB
stdin
Standard input is empty
stdout
Collection says: 6