August 26, 2016 Re: C# 7 Features - Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | On Thursday, 25 August 2016 at 14:43:35 UTC, Dominikus Dittes Scherkl wrote:
> But I dislike the named tuple members.
> Why not declare them at the calling site?
>
> (int, int, int, string) fn()
> {
> return (3, 2, 1, "meins");
> }
Because how are you supposed to know what each member of the tuple represents? If you read the function signature all you see is "int, int, int, string".
|
September 05, 2016 Re: C# 7 Features - Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dominikus Dittes Scherkl | On Thursday, 25 August 2016 at 14:43:35 UTC, Dominikus Dittes Scherkl wrote:
>
> (int, int, int, string) fn()
> {
> return (3, 2, 1, "meins");
> }
>
> int x, y, z;
> string s;
> (x, y, z, s) = fn();
Another solution is to support out argument declarations, as they are a more general feature. These could then be used as follows:
Tuple!(int, string) fn();
void unpack(T...)(Tuple!T, out T decls); // new phobos function
fn().unpack(int i, string s);
I think a combination of tuple slicing and unpack() overloads could allow ignoring leading or trailing tuple fields.
|
September 05, 2016 Re: C# 7 Features - Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Treleaven | On Monday, 5 September 2016 at 15:43:43 UTC, Nick Treleaven wrote: > Another solution is to support out argument declarations, as they are a more general feature. These could then be used as follows: > > Tuple!(int, string) fn(); > void unpack(T...)(Tuple!T, out T decls); // new phobos function > > fn().unpack(int i, string s); > > I think a combination of tuple slicing and unpack() overloads could allow ignoring leading or trailing tuple fields. We can already (almost do that): ======================================================== import std.stdio, std.typecons; void unpack(T...)(Tuple!T tup, out T decls) { static if (tup.length > 0) { decls[0] = tup[0]; tuple(tup[1..$]).unpack(decls[1..$]); } } void main() { auto t = tuple(1, "a", 3.0); int i; string s; double d; t.unpack(i, s, d); writeln(i); writeln(s); writeln(d); } ======================================================== |
September 06, 2016 Re: C# 7 Features - Tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lodovico Giaretta | On Monday, 5 September 2016 at 15:50:31 UTC, Lodovico Giaretta wrote:
> On Monday, 5 September 2016 at 15:43:43 UTC, Nick Treleaven wrote:
> We can already (almost do that):
>
> ========================================================
> import std.stdio, std.typecons;
>
> void unpack(T...)(Tuple!T tup, out T decls)
> {
> static if (tup.length > 0)
> {
> decls[0] = tup[0];
> tuple(tup[1..$]).unpack(decls[1..$]);
> }
> }
>
> void main()
> {
> auto t = tuple(1, "a", 3.0);
> int i;
> string s;
> double d;
> t.unpack(i, s, d);
> writeln(i);
> writeln(s);
> writeln(d);
> }
The main benefit of supporting tuple syntax is unpacking into new declarations (writing Tuple!(...) or tuple!(...) isn't that significant IMO). I was suggesting that out argument *declarations* actually provides this and is a more general feature.
|
Copyright © 1999-2021 by the D Language Foundation