Skip to content

Instantly share code, notes, and snippets.

@bioinfornatics
Created March 4, 2012 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bioinfornatics/1969776 to your computer and use it in GitHub Desktop.
Save bioinfornatics/1969776 to your computer and use it in GitHub Desktop.
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] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment