Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.40 KB | None | 0 0
  1. module mytuple;
  2.  
  3. import dranges.templates;
  4. import dranges.typetuple;
  5. import std.traits;
  6. import std.conv;
  7.  
  8. template containsStrings( T... ) {
  9.     enum containsStrings = StaticFilter!( isSomeString, StaticMap!( TypeOf, StaticFilter!( isAlias, T ) ) ).length != 0;
  10. }
  11.  
  12. mixin template TupleNameAccess( int index, string name, T... ) {
  13.     mixin( "ref auto " ~ name ~ "( ) { return t.field[" ~ to!string(index) ~ "]; }" );
  14.     static if ( T.length > 0 ) {
  15.         mixin TupleNameAccess!( index + 1, T );
  16.     }
  17. }
  18.  
  19. struct Tuple( T... ) if ( containsStrings!T ) {
  20.     alias StaticFilter!( isType, T ) Types;
  21.     alias StaticFilter!( isAlias, T ) Names;
  22.     Tuple!Types t;
  23.     alias t this;
  24.    
  25.     this( Types args ) {
  26.         t = typeof( t )( args );
  27.     }
  28.    
  29.     mixin TupleNameAccess!( 0, Names );
  30. }
  31.  
  32. struct Tuple( T... ) if ( !containsStrings!T ) {
  33.     T field;
  34.     alias field expand;
  35.     //alias field this;
  36.    
  37.     enum length = T.length;
  38.    
  39.     this( T args ) {
  40.         field = args;
  41.     }
  42.    
  43.     Tuple!( T, U ) opBinary( string op : "~", U... )( Tuple!U arg ) {
  44.         return Tuple!( T, U )( expand, arg.expand );
  45.     }
  46. }
  47.  
  48. Tuple!T tuple( T... )( T args ) {
  49.     return Tuple!T( args );
  50. }
  51.  
  52. unittest {
  53.     auto a = tuple( 1, 'a' );
  54.     auto b = tuple( 1.2, "b" );
  55.     auto c = a ~ b;
  56.     assert( c == tuple( 1, 'a', 1.2, "b" ) );
  57.    
  58.     auto o = Tuple!( int, "a", string, "b" )( 1, "a" );
  59.     auto n = Tuple!( int, "r", string, "g" )( 1, "a" );
  60.     assert( o == n );
  61. }
  62.  
  63. void main( ) {
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement