D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4666 - Optional name for std.typecons.Tuples?
Summary: Optional name for std.typecons.Tuples?
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: Andrei Alexandrescu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-17 07:31 UTC by bearophile_hugs
Modified: 2016-10-14 01:48 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-08-17 07:31:10 UTC
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.
Comment 1 Andrei Alexandrescu 2010-10-07 08:00:27 UTC
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.
Comment 2 bearophile_hugs 2010-10-07 13:23:09 UTC
(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.
Comment 3 bearophile_hugs 2010-10-16 15:34:55 UTC
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)
}
Comment 4 Andrei Alexandrescu 2016-10-14 01:48:02 UTC
This won't happen at this point.