www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Tuples and array literals

reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
I tried to search the ng and dstress test cases, but there was nothing
about these.

So here are the working ones:

  const a = [1,2];
  int[] b = [1,2];

  struct foo {
    const int[] c = [1,2];
    const d = [1,2];
  }

  void main() {
    const e = [1,2];
  }

These ones don't work:

  template Tuple(E...) { alias E Tuple; }
  alias Tuple!(0,1) TP;

  const a = [ TP ];
  int[] b = [ TP ];

  struct foo {
    const int[] c = [ TP ];
    const d = [ TP ];
  }

So, are these going to be fixed in future releases or is there some
serious limitation in the template/tuple system that prevents any of
these? Any workarounds that make use of tuples?
Mar 06 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Jari-Matti Mäkelä wrote:
 I tried to search the ng and dstress test cases, but there was nothing
 about these.
 
 So here are the working ones:
 
   const a = [1,2];
   int[] b = [1,2];
 
   struct foo {
     const int[] c = [1,2];
     const d = [1,2];
   }
 
   void main() {
     const e = [1,2];
   }
 
 These ones don't work:
 
   template Tuple(E...) { alias E Tuple; }
   alias Tuple!(0,1) TP;
 
   const a = [ TP ];
   int[] b = [ TP ];
 
   struct foo {
     const int[] c = [ TP ];
     const d = [ TP ];
   }
 
 So, are these going to be fixed in future releases or is there some
 serious limitation in the template/tuple system that prevents any of
 these? Any workarounds that make use of tuples?
I'm not an expert on Tuples, but I don't think you can alias expression tuples like (0,1), for the same reason you can't alias 1 or 2. The solution is the same: put it in a constant. typeof(Tuple!(0,1)) TP = Tuple!(0,1); The problem here is that I'm not sure if assigning between tuples works, either. I remember having to write a loop to do it...
 foreach( i, e ; tuple )
     TP[i] = e;
To be honest, I haven't really played with expression tuples all that much, so I could be totally off-base. None the less, something to think about :) -- Daniel -- Unlike Knuth, I have neither proven or tried the above; it may not even make sense. v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 06 2007
parent =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Daniel Keep kirjoitti:
 
 Jari-Matti Mäkelä wrote:
 I tried to search the ng and dstress test cases, but there was nothing
 about these.

 So here are the working ones:

   const a = [1,2];
   int[] b = [1,2];

   struct foo {
     const int[] c = [1,2];
     const d = [1,2];
   }

   void main() {
     const e = [1,2];
   }

 These ones don't work:

   template Tuple(E...) { alias E Tuple; }
   alias Tuple!(0,1) TP;

   const a = [ TP ];
   int[] b = [ TP ];

   struct foo {
     const int[] c = [ TP ];
     const d = [ TP ];
   }

 So, are these going to be fixed in future releases or is there some
 serious limitation in the template/tuple system that prevents any of
 these? Any workarounds that make use of tuples?
I'm not an expert on Tuples, but I don't think you can alias expression tuples like (0,1), for the same reason you can't alias 1 or 2.
I'm not an expert either. I found the aliasing from http://www.digitalmars.com/d/tuple.html. "Expression Tuples If a tuple's elements are solely expressions, it is called an ExpressionTuple." "It can be used to create an array literal: alias Tuple!(3, 7, 6) AT; ... int[] a = [AT]; // same as [3,7,6]" --- I forgot to mention that in the code I sent b) actually works, but a) doesn't: template Tuple(E...) { alias E Tuple; } alias Tuple!(0,1) TP; const a = [ TP ]; // example a) void main() { static const b = [ TP ]; // example b) } I have used code similar to b) a lot to precalculate tables in my code. I was just curious about the actual reasons to deny the use of a). AFAIK, it can be calculated in compile time and there is also evidence that the tuple in a literal syntax works elsewhere. Something like template Tuple(E...) { alias E Tuple; } alias Tuple!(0,1) TP; const a = [ TP[0], TP[1] ]; also works, but the tuples really should have variable length in my code.
 
 The solution is the same: put it in a constant.
 
   typeof(Tuple!(0,1)) TP = Tuple!(0,1);
Ok, I have to try this. Thanks. My first impression is that it does not work :) test.d(2): Error: forward reference to type (int, int) test.d(2): Error: cannot implicitly convert expression (tuple(0,1)) of type (int, int) to (int, int) Error: cannot cast int to (int, int)
Mar 07 2007