Skip to content

@bioinfornatics /gist:1969776
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.