A Discussion of Tuple Syntax

ixid nuaccount at gmail.com
Mon Aug 19 15:08:25 PDT 2013


On Friday, 16 August 2013 at 23:48:53 UTC, bearophile wrote:
> Meta:
>
>> Andrei/Walter didn't want to merge that pull request without a 
>> full consideration of the different design issues involved, 
>> which in retrospect was a good decision.
>
> I agree that before adding some new syntax you have to think 
> well.
>
>
>> - {a, b} is not as pretty, but it's not that bad of an 
>> alternative (though it may still have issues as well)
>
> It has technical issues, they were discussed in one of the last 
> threads. Such problems should be added in a note in the DIP32, 
> so they don't get lost in time.
>
>
>> - #(a, b) is unambiguous and would probably be the easiest 
>> option. I don't think it looks too bad, but some people might 
>> find it ugly and noisy
>
> It looks nice (but I don't know if it's technically usable), I 
> have added it at the end of the list of alternative syntaxes:
> http://wiki.dlang.org/DIP32#Use_case_of_uniform_tuple_syntax
>
>
>> I personally think #(a, ?) or #(a, *) would be best, but all 
>> that's  really necessary is a symbol that cannot also be an 
>> identifier.
>
> I think ? is clear, unambiguous, etc, and I like it. The * is 
> already used for pointers, product, dereferencing, ** is used 
> for pow, so it's better to not add further meanings to it.
>
>
>> - Concatenating tuples with ~. This is nice to have, but not 
>> particularly important.
>
> In theory I like this operation, but in practice in my D code I 
> don't need it often, so I think it should be left out, for 
> later.
> In Python I sometimes concatenate tuples, but in such use cases 
> they are essentially immutable dynamically typed arrays, losing 
> their positional meaning.
>
>
> Tuples could also be used in switch/case statements, to support 
> a very basic form of pattern matching. See also a standard 
> method named "unapply" I have discussed a bit here, coming from 
> Scala language, that is a very simple solution to solve a 
> significant problem:
> http://d.puremagic.com/issues/show_bug.cgi?id=596
>
> Bye,
> bearophile

What about using : as people had intended to use the comma 
operator?
uint a, b;
(a : b) = (1 : 2);
(a : b) = tupleReturningFunction;
auto c = (1 : (2 : 3)); // A tuple containing a tuple
auto d = (1 : ((2 : 3) : (4 : 5))); // a tuple with a tuple of 
tuples as a member

auto assoc_array = [1 : (2 : 3)]; // A tuple associative value
auto assoc_array2 = [(1 : 2) : 3]; // A tuple associative key

auto ternary = value? (1 : 2) : (3 : 4); // tuple values in a 
ternary operator

It's nicer looking than #().


More information about the Digitalmars-d mailing list