www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C# 7 Features - Tuples

reply dom <dschoerk gmx.at> writes:
https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

came across the new c# features today. I really liked the syntax 
for Tuples (and deconstructors), would be great to have a similar 
syntax in D :)
Aug 25 2016
next sibling parent Suliman <evermind live.ru> writes:
On Thursday, 25 August 2016 at 13:41:29 UTC, dom wrote:
 https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

 came across the new c# features today. I really liked the 
 syntax for Tuples (and deconstructors), would be great to have 
 a similar syntax in D :)
Yeah, it's looks very cool!
Aug 25 2016
prev sibling next sibling parent reply Dominikus Dittes Scherkl <Dominikus.Scherkl continental-corporation.com> writes:
On Thursday, 25 August 2016 at 13:41:29 UTC, dom wrote:
 https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

 came across the new c# features today. I really liked the 
 syntax for Tuples (and deconstructors), would be great to have 
 a similar syntax in D :)
Pretty obvious syntax, but would require the comma operator to be removed. Isn't it deprecated long enough meanwhile, so we finally can kill it? 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"); } int x, y, z; string s; (x, y, z, s) = fn();
Aug 25 2016
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/25/16 10:43 AM, Dominikus Dittes Scherkl wrote:
 On Thursday, 25 August 2016 at 13:41:29 UTC, dom wrote:
 https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/


 came across the new c# features today. I really liked the syntax for
 Tuples (and deconstructors), would be great to have a similar syntax
 in D :)
Pretty obvious syntax, but would require the comma operator to be removed. Isn't it deprecated long enough meanwhile, so we finally can kill it?
It was *just* deprecated, I don't think even in a released version yet! -Steve
Aug 25 2016
parent reply Seb <seb wilzba.ch> writes:
On Thursday, 25 August 2016 at 15:02:06 UTC, Steven Schveighoffer 
wrote:
 On 8/25/16 10:43 AM, Dominikus Dittes Scherkl wrote:
 On Thursday, 25 August 2016 at 13:41:29 UTC, dom wrote:
 https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/


 came across the new c# features today. I really liked the 
 syntax for
 Tuples (and deconstructors), would be great to have a similar 
 syntax
 in D :)
Pretty obvious syntax, but would require the comma operator to be removed. Isn't it deprecated long enough meanwhile, so we finally can kill it?
It was *just* deprecated, I don't think even in a released version yet! -Steve
Yep, https://dlang.org/deprecate.html#Using%20the%20result%20of%20a%20comma%20expression https://github.com/dlang/dmd/pull/5737 However working out the rules and changes for a Tuple syntax would be a great topic for a DIP: https://github.com/dlang/DIPs Btw there has been previous work on this topic, so this could be used as a base: https://wiki.dlang.org/DIP32
Aug 25 2016
parent jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 25 August 2016 at 15:50:09 UTC, Seb wrote:
 https://dlang.org/deprecate.html#Using%20the%20result%20of%20a%20comma%20expression
 https://github.com/dlang/dmd/pull/5737

 However working out the rules and changes for a Tuple syntax 
 would be a great topic for a DIP:

 https://github.com/dlang/DIPs

 Btw there has been previous work on this topic, so this could 
 be used as a base:

 https://wiki.dlang.org/DIP32
I like DIP32, but I wouldn't be that bothered if it took until 2018 to get something like it in D.
Aug 25 2016
prev sibling next sibling parent rumbu <rumbu rumbu.ro> writes:
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");
 }

 int x, y, z;
 string s;
 (x, y, z, s) = fn();
This is possible: " (string, string, string) LookupName(long id) // tuple return type { ... // retrieve first, middle and last from data storage return (first, middle, last); // tuple literal } ... "You can also deconstruct into existing variables with a deconstructing assignment: (first, middle, last) = LookupName(id2); // deconstructing assignment"
Aug 25 2016
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 25.08.2016 16:43, Dominikus Dittes Scherkl wrote:
 On Thursday, 25 August 2016 at 13:41:29 UTC, dom wrote:
 https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/


 came across the new c# features today. I really liked the syntax for
 Tuples (and deconstructors), would be great to have a similar syntax
 in D :)
Pretty obvious syntax, but would require the comma operator to be removed. Isn't it deprecated long enough meanwhile, so we finally can kill it? 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"); } int x, y, z; string s; (x, y, z, s) = fn();
I don't understand. What is the issue here?
Aug 25 2016
prev sibling next sibling parent Kagamin <spam here.lot> writes:
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();
See https://forum.dlang.org/post/ubrngkdmyduepmfkhefp forum.dlang.org
Aug 26 2016
prev sibling next sibling parent Cauterite <cauterite gmail.com> writes:
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".
Aug 26 2016
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
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.
Sep 05 2016
parent reply Lodovico Giaretta <lodovico giaretart.net> writes:
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); } ========================================================
Sep 05 2016
parent Nick Treleaven <nick geany.org> writes:
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.
Sep 06 2016
prev sibling parent ixid <adamsibson hotmail.com> writes:
On Thursday, 25 August 2016 at 13:41:29 UTC, dom wrote:
 https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

 came across the new c# features today. I really liked the 
 syntax for Tuples (and deconstructors), would be great to have 
 a similar syntax in D :)
This is fantastic, hopefully we can have this syntax in D.Ultimately it would be nice if we could move towards the rest of the C-style tidy ups that are becoming more commonly used but I know this community is pretty conservative. Optional semi-colons, no parens on if, while etc and enforced curly braces. Also := being a synonym for auto assignment.
Aug 26 2016