www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4666] New: Optional name for std.typecons.Tuples?

reply d-bugmail puremagic.com writes:
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



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: -------
Aug 17 2010
next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Oct 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4666





 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: -------
Oct 07 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4666




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: -------
Oct 16 2010