Tuple literal syntax

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Oct 10 07:58:05 PDT 2010


On 10/10/10 9:00 CDT, Denis Koroskin wrote:
> On Sun, 10 Oct 2010 17:39:52 +0400, Stephan Soller
> <stephan.soller at helionweb.de> wrote:
>
>> On 07.10.2010 17:36, Andrei Alexandrescu wrote:
>>> On 10/7/10 10:13 CDT, Kagamin wrote:
>>>> Andrei Alexandrescu Wrote:
>>>>
>>>>> struct Coord
>>>>> {
>>>>> int x, y, z;
>>>>> }
>>>>>
>>>>> one iota typesafer than
>>>>>
>>>>> alias Tuple!(int, "x", int, "y", int, "z") Coord;
>>>>
>>>> Is there a real need for an alternative way to declare structs?
>>>
>>> Yes.
>>>
>>> Andrei
>>
>> I agree but I also don't agree. I like the possibility to return
>> multiple values (quite handy in Ruby, PHP and other languages) and
>> therefore we need language constructs to group multiple values and
>> ungroup them again.
>>
>> However as soon as these fields are named it looks to me like an
>> ordinary structure. So why not just create something like an
>> "anonymous structure literal" that behaves to structures like
>> anonymous functions (or delegates) to functions?
>>
>> This would look a lot more consistent to me than playing around with
>> different kinds of tuples. However I'm not sure about the details
>> (e.g. how would such structures handle type or alias contents).
>>
>> Happy programming
>> Stephan
>
> Exactly. To be honest, I see absolutely no reason to have yet another to
> declare structs, but some people have other preferences.
> I don't mind having Tuple in a library, as long as I'm not insisted in
> using it *and* it doesn't affect other language features.
>
> I mean, it's not even shorter to write
>
> alias Tuple!(int, "x", int, "y", int, "z") Coord;
>
> than
>
> struct Coord { int x; int y; int z; }

Tuples with named fields are restricted in a number of ways compared to 
structs. They are expandable, they have indexed fields, they offer no 
encapsulation, etc. Lambda structs would be rather unwieldy. I 
personally am fine with tuples.

Andrei


More information about the Digitalmars-d mailing list