Bug 4128 – Named-fields Tuple assign from unnamed-fields Tuple

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-04-25T05:41:00Z
Last change time
2016-10-14T01:35:54Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-04-25T05:41:46Z
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 by andrei — 2016-10-14T01:35:54Z
A PR recently added this facility.