A Discussion of Tuple Syntax

Kenji Hara k.hara.pg at gmail.com
Tue Aug 20 18:38:04 PDT 2013


2013/8/21 Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org>

> I think this whole thread is approaching things wrongly. It should be:
>
> 1. What do we need?
>

Both
a. Built-in tuple construction syntax
and
b. Built-in tuple deconstruction syntax

2. What does a solution look like in the current language?
>

a.
  alias Types = {int, long};
  auto values = {1, "str"};
b.
  auto {a, b} = tup;  // on variable declaration
  {a, b} = tup;       // on assignment expression
  ...

3. Which parts of the solution are frequent/cumbersome enough to warrant a
> language change?
>

a1. To make std.typetuple.TypeTuple template unnecessary.
a2. To keep std.typecons.Tuple as-is.
a3. To make expression tuple more usable.

  Today making the built-in tuple of runtime values needs unavoidable
runtime cost.

  int x, y;
  auto tup = TypeTuple!(x+1, y+2);
  // NG, template cannot take x+1 and y+2 because they are
  // expressions which cannot be evaluated in compile time
  auto tup = tuple(x+1, y+2);
  // NG, tup is not a builti-in tuple (it is an object of
std.typecons.Tuple struct).
  auto tup = tuple(x+1, y+2).expand;
  // OK, tup is a builti-in tuple. But we cannot avoid the cost to pack
in std.typecons.Tuple.

  Built-in tuple syntax would solve the issue.

  auto tup = {x+1, y+2};
  // tup is a built-in tuple, and it would be equivalent with:
  // auto tup[0] = x+1, tup[1] = x*2; // pseudo code
  // so no packing cost in runtime is there.

b.
  To make deconstruction code more readable.

  auto tup = std.typecons.tuple(1, "str");
  auto a = tup[0], b = tup[1];    // Currrent:
  // Deconstruct two fields in tup to the two variables a and b
  auto {a, b} = tup;    // New syntax:
  // Deconstruct syntax would recognize alias this tuple,
  // and expand it automatically.

Kenji Hara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130821/5627eb6d/attachment.html>


More information about the Digitalmars-d mailing list