What is the stance on partial initializers when declaring

kenji hara k.hara.pg at gmail.com
Fri Jul 22 02:56:24 PDT 2011


I have posted a pull request for library tuple (= alias this tuple)
unpacking somewhere:
https://github.com/D-Programming-Language/dmd/pull/74
(Test code) https://github.com/D-Programming-Language/dmd/pull/74/files#L7R130

This patch allows unpacking tuple following places:
- Non-template function arguments
- Template function arguments
- StructLiteral arguments
- Initializer
- foreach aggregate
- foreach range.front

And I think make enhancement patch allowing tuple declaration like follows:
auto (i, s) = tuple(1, "str");
TypeTuple!(string, int[]) (s, arr) = TypeTuple!("str", [1, 2]);

Syntax:
("auto" | TupleTypeName) "(" Identifier ["," Identifier ...] ")" "="
Initializer ";"

Tuple assignment is already supported in current D like follows:

import std.typetuple : seq = TypeTuple;
void main()
{
  int x, y;
  seq!(x, y) = seq!(1, 2);  // aassign
  assert(x == 1);
  assert(y == 2);
}

I think more language support for tuple assignment is not need.

Kenji Hara

2011/7/22 bearophile <bearophileHUGS at lycos.com>:
> Simen Kjaeraas:
>
>> I believe the syntax that came out on
>> top in earlier discussions was the upended hamburger bun, or banana
>> syntax:
>>
>> (| float f, string s |) foo = (| 1.2, "Eh, whut?" |);
>
> I was the one to suggest this syntax. Andrei told me this is named banana syntax.
> There are also alternatives:
> (| auto f, auto s |) foo = (| 1.2, "Eh, whut?" |);
> auto (| f, s |) foo = (| 1.2, "Eh, whut?" |);
>
>
>> Even if the comma operator were removed and that syntax used for
>> tuples, Nick's suggested syntax would not cause ambiguities.
>
> I like the banana syntax, but I think some people don't like it. So as second choice there is a Python-style tuple syntax.
>
> -----------------
>
> Andrei:
>
>> Here's a crazy idea:
>>
>> auto foo = tuple(1.2, "Eh, whut?");
>
> This was in my original answer:
>>(especially for their unpacking),<
>
> The main point of having some tuple syntax sugar is to allow a good enough unpacking. Currently it's not good enough for a language as D that wants to be functional too. Functional languages use tuples often.
> If you are also able to improve the literals too, then it's even better.
>
> A return tuple is better than ref/out arguments, as used in C/C++ languages. It's simpler to read because it's easy to see it's the result of a function. Even Go language, that is otherwise quite minimal, has multiple return values.
>
>
> An example usage:
> http://rosettacode.org/wiki/Sokoban#D
>
>
> The C++0x version:
>
> vector<vector<char>> temp, cur = get<0>(open.front());
> string cSol = get<1>(open.front());
> int x = get<2>(open.front());
> int y = get<3>(open.front());
> open.pop();
>
>
> D2 versione with Tuple:
>
> auto item = open.pop();
> CTable cur = item[0];
> string cSol = item[1];
> const int x = item[2];
> const int y = item[3];
>
>
> Python version:
>
> cur, csol, x, y = open.popleft()
>
>
> One possible D syntax:
>
> const (|cur, cSol, x, y|) = open.pop();
>
> Some tuple unpacking syntax in D is useful in D.
> There are other useful purposes, like unpacking in foreach:
>
> foreach ((|x, y|); zip([1, 2, 3], "abc") {}
>
>
> Python3 shows more syntax, to unpack only the first items of a tuple, but this is less important than a basic unpacking syntax:
>
> foo():
>    return 1, 2, 3, 4
> a, b, *somemore = foo()
>
> Bye,
> bearophile
>


More information about the Digitalmars-d mailing list