public
Created

  • Download Gist
gistfile1.d
D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
import std.string;
import std.stdio;
import std.range;
 
mixin template arrayDelegator( alias instance, methods... ){
static if( methods.length > 0 ){
static if( methods[0] == "opIndexAssign" ){
mixin("
void opIndexAssign( " ~ ElementEncodingType!(typeof(instance)).stringof ~ " value, size_t index ){
array[index] = value;
}
");
}
else static if( methods[0] == "opIndex" ){
mixin(
ElementEncodingType!(typeof(instance)).stringof ~ " opIndex( size_t index ){
return instance[index];
}
");
}
else static if( methods[0] == "length" ){
mixin("
@property size_t length(){
return instance.length;
}
");
}
else
static assert( false );
static if( methods.length > 1 )
mixin arrayDelegator!( instance, methods[1 .. $ ] );
}
}
 
class Container{
size_t[] array;
 
mixin arrayDelegator!(array, "opIndexAssign", "opIndex", "length");
this( size_t[] a ){
array = a;
}
 
}
 
void main( string[] args ){
Container c = new Container( [0u, 1u, 2u, 3u] );
writeln( c[2] );
c[2] = 4u;
writeln( c[2] );
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.