www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4128] New: Named-fields Tuple assign from unnamed-fields Tuple

http://d.puremagic.com/issues/show_bug.cgi?id=4128

           Summary: Named-fields Tuple assign from unnamed-fields Tuple
           Product: D
           Version: future
          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-04-25 05:41:46 PDT ---
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").

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