A Discussion of Tuple Syntax
Meta
jared771 at gmail.com
Mon Aug 19 20:12:43 PDT 2013
Aggh, misposted. Let's try that again.
On Tuesday, 20 August 2013 at 02:51:20 UTC, Meta wrote:
> On Tuesday, 20 August 2013 at 01:06:28 UTC, H. S. Teoh wrote:
>> Actually, reading through DIP32 again, it sounds like Kenji is
>> proposing
>> the *same* syntax for both built-in tuples and
>> std.typecons.Tuple. In
>> the code example under "Generic type/expression tuple syntax",
>> he refers
>> to them respectively as "tuple type" and "tuple value". Mixing
>> is also
>> allowed (e.g., in the "alias Fields" line).
Maybe mixing should be disallowed, then, unless your tuple was
constructed from a variadic template argument list.
//Error. Tuple containing types
//at runtime doesn't make any sense
auto tup = #(1, int);
//Error
auto tup = #(int, string);
//Ok
auto tup = #(1, "a", true);
T[0] Test(T...)(T ts)
{
//Okay, each element of T can
//be statically inspected to ensure
//that you don't do something weird
auto tup = T;
//Also okay
alias tup = T;
//Error
auto tup = #(int, string, bool);
//Ok
alias tup = #(int, string, bool);
}
>> There might be some areas where this conflation may cause
>> trouble,
>> ...
>>
>> Actually, looking at this again, it seems the problem is with
>> the "tup = {1; x}" line. Does {1; x} in the above code mean
>> {1; 123}, or does it mean {1; {alias of x}}? For example, if
>> you wrote this:
>>
>> int x=123;
>> auto tup = {1; x};
>> x++;
>> writeln(tup);
>>
>> What should be the output?
I'd say that x++ would not modify the x within the tuple. It's
the same as:
int x = 123;
auto y = x;
x++;
writeln(y);
Y is not changed, of course. However, if the tuple contains a
reference type, such as a slice, then it would be modified.
int[] x = [123];
auto tup = #(1, x);
x[]++;
//Prints #(1, [124])
writeln(tupe);
>> Should the output change if the second line
>> is changed to:
>>
>> alias tup = {1; x};
>>
>> ?
I don't think this should be possible. It would be the same as
doing:
alias two = 3; //Error
Unless the tuple contained only types. Then it would be possible.
alias intAndStr = #(int, string); //Ok
The semantics are fairly easy when a tuple contains only values
and only types. The hard part is when it contains both values AND
types, so that should be severely limited, maybe only within
variadic templates.
I can't remember where this was mentioned... I think it was in
one of the other tuple threads I linked, and I think you brought
it up, Teo.
auto tup = #(1, "a");
alias tupType = typeof(tup);
While tup is a value, its type is #(int, string). In other words,
the type of a tuple value is a TypeTuple (alias tuple, whatever).
Which means the type of a tuple is a tuple itself, which is
pretty nifty.
More information about the Digitalmars-d
mailing list