Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module mytuple;
- import dranges.templates;
- import dranges.typetuple;
- import std.traits;
- import std.conv;
- template containsStrings( T... ) {
- enum containsStrings = StaticFilter!( isSomeString, StaticMap!( TypeOf, StaticFilter!( isAlias, T ) ) ).length != 0;
- }
- mixin template TupleNameAccess( int index, string name, T... ) {
- mixin( "ref auto " ~ name ~ "( ) { return t.field[" ~ to!string(index) ~ "]; }" );
- static if ( T.length > 0 ) {
- mixin TupleNameAccess!( index + 1, T );
- }
- }
- struct Tuple( T... ) if ( containsStrings!T ) {
- alias StaticFilter!( isType, T ) Types;
- alias StaticFilter!( isAlias, T ) Names;
- Tuple!Types t;
- alias t this;
- this( Types args ) {
- t = typeof( t )( args );
- }
- mixin TupleNameAccess!( 0, Names );
- }
- struct Tuple( T... ) if ( !containsStrings!T ) {
- T field;
- alias field expand;
- //alias field this;
- enum length = T.length;
- this( T args ) {
- field = args;
- }
- Tuple!( T, U ) opBinary( string op : "~", U... )( Tuple!U arg ) {
- return Tuple!( T, U )( expand, arg.expand );
- }
- }
- Tuple!T tuple( T... )( T args ) {
- return Tuple!T( args );
- }
- unittest {
- auto a = tuple( 1, 'a' );
- auto b = tuple( 1.2, "b" );
- auto c = a ~ b;
- assert( c == tuple( 1, 'a', 1.2, "b" ) );
- auto o = Tuple!( int, "a", string, "b" )( 1, "a" );
- auto n = Tuple!( int, "r", string, "g" )( 1, "a" );
- assert( o == n );
- }
- void main( ) {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement