Tuple [] operator

Ali Çehreli acehreli at yahoo.com
Mon Aug 8 13:32:03 PDT 2011


On Mon, 08 Aug 2011 15:55:38 -0400, Steven Schveighoffer wrote:

> On Mon, 08 Aug 2011 15:47:36 -0400, Christian Manning
> <cmanning999 at gmail.com> wrote:

[...]

> auto x = 1;
> Tuple!(int, short) a;
> 
> a[0] = 1;
> switch(x)
> {
> case 0:
>     a[0] = 2;
>     break;
> case 1:
>     a[1] = 2;

Those assignments are now bound at compile time.

>     break;
> default:
>     assert(0, "does not compute!");
> }
> 
> the point is, the compiler has no idea what the lvalue expression's type
> should be when you do:
> 
> a[x] = 1;
> 
> is it short or int?
> 
> so the compiler must *know* what type x is at compile time in order for
> this to be valid.

I think it's more import for the compiler to know what type a[x] is. The 
assignment operators of different types are different. On the other hand, 
I don't think a short vs int would make a difference when it comes to 
indexing (it shouldn't anyway).

> 
> -Steve

Ali


More information about the Digitalmars-d-learn mailing list