Thread overview
[Issue 4666] New: Optional name for std.typecons.Tuples?
August 17, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4666

           Summary: Optional name for std.typecons.Tuples?
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-08-17 07:31:10 PDT ---
This correct D2 program:

import std.stdio: writeln;
import std.typecons: Tuple;
void main() {
    alias Tuple!(double, "x", double, "y") Point2D;
    writeln(Point2D());
}


Prints (DMD 2.048):
Tuple!(double,"x",double,"y")(nan, nan)


In recent versions of the Python std lib there is collections.namedtuple, that is similar to std.typecons.Tuple: http://docs.python.org/dev/library/collections.html#collections.namedtuple

A namedtuple contains a name too, this is handy when they get printed.

So in theory a Tuple may allow an optional name too:


import std.stdio: writeln;
import std.typecons: Tuple;
void main() {
    alias Tuple!("Point2D", double, "x", double, "y") Point2D;
    writeln(Point2D());
}


That may print:
Point2D(nan, nan)


The main difference between the Tuple named 'Point2D' and a normal struct named 'Point2D' is the extra features Tuples have (and will have) over plain structs.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4666


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


--- Comment #1 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-10-07 08:00:27 PDT ---
Hm. Fair point, though I fear it could create more confusion than convenience. After all anyone can define a function that prints the tuple however they want.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4666



--- Comment #2 from bearophile_hugs@eml.cc 2010-10-07 13:23:09 PDT ---
(In reply to comment #1)
> Hm. Fair point, though I fear it could create more confusion than convenience.

This was just a suggestion, it's not an important thing.

If Tuples get more compiler support and become more like structural types, then the optional name is able to turn them back again into nominal typing (if their name differs, they need to be considered different types, even if everything else is the same).


> After all anyone can define a function that prints the tuple however they want.

This is surprisingly uncommon in Python (and I think in D too). When you need a special printing function you quite probably prefer to define a struct with opString instead a free function that accepts a tuple of a certain type.

This means that default printing of tuples needs to be good.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 16, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4666



--- Comment #3 from bearophile_hugs@eml.cc 2010-10-16 15:34:55 PDT ---
It's important to give a good textual representation to Tuples, to avoid a very cluttered textual output. On the other hand defining a printing function to print tuples is not so natural.

So an alternative solution is to give each Tuple a mutable static string
argument, that for a Tuple defined as:
Tuple!(double, "x", double, "y")

gets automatically initialized to something like:
"Tuple!(double,\"x\",double,\"y\")"

But later may be modified by the programmer to something different. The tuple toString may just always prepend this string to the textual representation of the Tuple.

Allowing something like this:


void main() {
import std.stdio: writeln;
import std.typecons: Tuple;
void main() {
    alias Tuple!(double, "x", double, "y") Point;
    writeln(p.__name); // prints: Tuple!(double,"x",double,"y")
    auto p = Point(1, 2);
    writeln(p); // prints: Tuple!(double,"x",double,"y")(1, 2)
    Point.__name = "Point";
    writeln(p.__name); // prints: Point
    writeln(p); // prints: Point(1, 2)
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------