A Discussion of Tuple Syntax

Meta jared771 at gmail.com
Mon Aug 19 16:18:08 PDT 2013


On Monday, 19 August 2013 at 18:43:37 UTC, Meta wrote:
>> What does concatenating a tuple actually do?  That is:
>> auto a = #(1,2) ~ 3; //Result: a == #(1,2,3), right?
>> auto b = a ~ #(4,5); //Is  b == #(1,2,3,#(4,5)) or is b == 
>> #(1,2,3,4,5)?
>
> I think it should work the same as with arrays. So:
>
> auto a = #(1, 2) ~ 3; //Error: 3 is not a tuple
> auto a = #(1, 2) ~ #(3); //Result: #(1, 2, 3), just like an 
> array
>
> auto b = a ~ #(4, 5); //Result: #(1, 2, 3, 4, 5). Again, like 
> arrays.

I was being dumb, [1, 2] ~ 3 is of course valid for arrays, so 
that should work for tuples as well. Furthermore, #(1, 2) ~ "a" 
should be valid, and will result in #(1, 2, "a").

> I think keeping the same semantics as arrays would be the best 
> way to do it. I think it nicely follows the principle of least 
> astonishment. If you wanted to explicitly append a tuple and 
> have it nested, you'd need to do:
>
> auto b = a ~ #(#(4, 5));
>
> Which is messy, but at least it's explicit about what is going 
> on.

The tricky part of this that I forgot to mention is that you 
can't do this with arrays, so we're in unknown territory, but I 
think this is sensible. Again, it's quite ugly, but oh well.


More information about the Digitalmars-d mailing list