A Discussion of Tuple Syntax

bearophile bearophileHUGS at lycos.com
Tue Aug 20 09:20:54 PDT 2013


Kenji Hara:

> My position to such a feature is constant.

Even if your opinion is not changed, I have to show that common 
tuple-related features to the other persons that are reading this 
thread, because it's an an idea to keep in account (even if it's 
refused), and some other person could have an opinion different 
from yours.


> However, array unpacking would require *hidden* runtime 
> boundary check.
> In above example code,
>
>   assert(tup[2].length == 2);
>   // or
>   if (tup[2].length != 2) throw Error("Runtime mismatch of 
> unpacking
> pattern");
>   // or similar
>
> should be implicitly inserted by compiler, even in @system code.
> I'm not sure that is acceptable cost or not.

Runtime boundary checks and tests are not needed if you unpack a 
fixed-sized array:

auto tup = Tuple!(int, string[2])(1, ["red", "blue"]);
auto {x, [c1, c2]} = tup;

Regarding the de-structuring of dynamic arrays, I think it's not 
too much different than this:

void main() {
     int[] a = [10, 20, 30];
     int[2] b = a;
}

If you compile and run it without switches, you get at run-time:

object.Error: lengths don't match for array copy, 2 = 3


While I receive no run-time errors if I compile with 
-noboundscheck.

Bye,
bearophile


More information about the Digitalmars-d mailing list