D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4128 - Named-fields Tuple assign from unnamed-fields Tuple
Summary: Named-fields Tuple assign from unnamed-fields Tuple
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-04-25 05:41 UTC by bearophile_hugs
Modified: 2016-10-14 01:35 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-04-25 05:41:46 UTC
D type system is mostly nominative:
http://en.wikipedia.org/wiki/Nominative_type_system

But in some situations a bit of structural type system can be useful, to make the language or its standard library more flexible.

One of such situations is the std.typecons.Tuple, it can be solved with limited changes in the opOpAssign(string Op:"~=")() method of Tuple.

You can see the problem here, I can define an array of Tuple and I can specify the names of the fields (here 'x' and 'y') for a nicer usage of the fields:


import std.typecons: Tuple, tuple;
void main() {
    Tuple!(int, "x", int, "y")[] arr;
    arr ~= Tuple!(int, "x", int, "y")(10, 20); // OK

    alias Tuple!(int, "x", int, "y") IntPair;
    arr ~= IntPair(10, 20); // OK

    arr ~= tuple(10, 20); // line 9, Error
}


The line 9 is handy, and natural to write, but it's not correct, dmd 2.043 prints:
test.d(9): Error: cannot append type Tuple!(int,int) to type Tuple!(int,"x",int,"y")[]

In my opinion in this situation the assign at line 9 can be accepted because the Tuple of two ints with no field names can be seen as more generic and compatible to the Tuple with two named int fields.

On the other hand a line like the following one can be seen as incompatible still:

arr ~= Tuple!(int, "z", int, "w")(10, 20);

Because this Tuple is not more general than Tuple!(int, "x", int, "y").
Comment 1 Andrei Alexandrescu 2016-10-14 01:35:54 UTC
A PR recently added this facility.