Thread overview
[Issue 538] New: Can't return an expression tuple from a function
Nov 17, 2006
d-bugmail
Nov 18, 2006
d-bugmail
Nov 18, 2006
d-bugmail
Nov 18, 2006
d-bugmail
Nov 18, 2006
d-bugmail
Jan 02, 2007
d-bugmail
Nov 26, 2010
Leandro Lucarella
Nov 26, 2010
nfxjfg@gmail.com
Jan 20, 2013
Andrej Mitrovic
November 17, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=538

           Summary: Can't return an expression tuple from a function
           Product: D
           Version: 0.174
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: wbaxter@gmail.com


You can't return an expression tuple from a function.
Right now, if you try to return one:

  template Tuple(T...) { alias T Tuple; }
  alias Tuple!(int,int) TType;

  TType foo()
  {
      TType x;  x[0] = 1; x[1] = 2;
      return x;
  }

You get the error:
  "Error: cannot implicitly convert expression
(tuple((_x_field_0),(_x_field_1))) of type (int, int) to (int, int)"


-- 

November 18, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=538





------- Comment #1 from brunodomedeiros+bugz@gmail.com  2006-11-18 09:13 -------
Not a bug. "A Tuple is not a type" (from http://www.digitalmars.com/d/template.html), so (unless stated otherwise) it cannot be used as the return value of a function. And it is not stated otherwise, the only special uses for tuples are "A Tuple can be used as an argument list to instantiate another template, or as the list of parameters for a function.".


-- 

November 18, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=538


wbaxter@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement




------- Comment #2 from wbaxter@gmail.com  2006-11-18 10:03 -------
I'll make it an enhancement.


-- 

November 18, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=538





------- Comment #3 from brunodomedeiros+bugz@gmail.com  2006-11-18 11:11 -------
Hum, you are quite more correct in your reasoning that I thought. When I made the previous post I had only read the new spec changes, which indicate what I said in the previous post. However upon reading http://www.digitalmars.com/d/tuple.html it seems the spec is incomplete. Tuples can indeed be used for more than just declaring function parameters.


-- 

November 18, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=538





------- Comment #4 from wbaxter@gmail.com  2006-11-18 12:49 -------
In any event, you are correct that it does not say anywhere that you should be able to return an expression tuple from a function.

On the other hand you can convert a struct to an expression tuple and initialize it's values by setting elements of the tuple.  So there really shouldn't be much difference at the metal level between returning a  struct{int x, int y}  and a Tuple!(int,int).  It should be doable as long as it's an expression tuple.

I tried for a while to write a simple list picking template.  I got it working for type tuples:

template Pick(TList...)
{
    template Indexes(int I, IList...)
    {
        static if (IList.length==0)
        {
            alias TList[I] Indexes;
        }
        else
        {
            alias Tuple!(TList[I], Indexes!(IList)) Indexes;
        }
    }

    template PickArgs(IList...) {
        alias With!(IList)  DList;
        DList PickArgs(TList arg) {
            DList darg;
            return darg;
        }
    }
}

    alias Tuple!(int,float,char[],long) ArgTSet;
    alias Tuple!(0,3,1) IdxSet;
    alias Pick!(ArgTSet) P;
    alias P.Indexes!(IdxSet) sublist;
    writefln(typeid(sublist));

Output:  int, long, float

But I couldn't figure out any way to do the equivalent with an Expression tuple without having return values.   Hmm, maybe it was just a bug that was causing the problem.

Here's my test for expression tuples:

    alias Tuple!(3,2.3,"hi",9) args;
    alias Pick!(args) P2;
    alias P2.Indexes!(IdxSet) subargs;
    writefln(subargs);

It fails to compile with a "tuple TList is used as a type".  But looking at it
now I think that may just be a compiler error.  I think maybe that should work.
 TList shouldn't have to be a type to define that alias.  (Otherwise alias
Tuple!(3,2.3,"hi",9) should fail too).


-- 

January 02, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=538


smjg@iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com
           Keywords|                            |diagnostic




------- Comment #5 from smjg@iname.com  2007-01-02 11:56 -------
That this doesn't work is one thing, but the error message ("Error: cannot
implicitly convert expression [...] of type (int, int) to (int, int)") is
certainly absurd.


-- 

November 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=538


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrei@metalanguage.com
         Resolution|                            |WONTFIX


--- Comment #6 from Andrei Alexandrescu <andrei@metalanguage.com> 2010-11-26 11:42:37 PST ---
This will not be fixed. D1 is frozen and D2 has Tuple.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=538


Leandro Lucarella <llucax@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |llucax@gmail.com


--- Comment #7 from Leandro Lucarella <llucax@gmail.com> 2010-11-26 14:13:03 PST ---
Is the error message improved? It's really misleading...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=538


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |nfxjfg@gmail.com
         Resolution|WONTFIX                     |


--- Comment #8 from nfxjfg@gmail.com 2010-11-26 14:58:06 PST ---
There's no reason to close this as WONTFIX. It's an backward compatible enhancement requests, and some of these have been answered by Walter in the past.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=538


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |andrej.mitrovich@gmail.com
            Version|D1                          |D2
         Resolution|                            |DUPLICATE


--- Comment #9 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-20 11:31:49 PST ---
Removed D1 as it will not receive any new enhancements.

The error message is now good for both D1 and D2.

As for D2, http://d.puremagic.com/issues/show_bug.cgi?id=6365 should implement tuple returns.

*** This issue has been marked as a duplicate of issue 6365 ***

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