What is the stance on partial initializers when declaring
bearophile
bearophileHUGS at lycos.com
Thu Jul 21 17:43:53 PDT 2011
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