Bug 538 – Can't return an expression tuple from a function
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2006-11-16T19:37:00Z
Last change time
2013-01-20T11:31:49Z
Keywords
diagnostic
Assigned to
nobody
Creator
wbaxter
Comments
Comment #0 by wbaxter — 2006-11-16T19:37:30Z
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)"
Comment #1 by bruno.do.medeiros+deebugz — 2006-11-18T09:13:39Z
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.".
Comment #2 by wbaxter — 2006-11-18T10:03:48Z
I'll make it an enhancement.
Comment #3 by bruno.do.medeiros+deebugz — 2006-11-18T11:11:00Z
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.
Comment #4 by wbaxter — 2006-11-18T12:49:47Z
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).
Comment #5 by smjg — 2007-01-02T11:56:13Z
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.
Comment #6 by andrei — 2010-11-26T11:42:37Z
This will not be fixed. D1 is frozen and D2 has Tuple.
Comment #7 by leandro.lucarella — 2010-11-26T14:13:03Z
Is the error message improved? It's really misleading...
Comment #8 by nfxjfg — 2010-11-26T14:58:06Z
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.
Comment #9 by andrej.mitrovich — 2013-01-20T11:31:49Z
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 ***