[Issue 10119] New: Add tuple overload which automatically captures the names of symbols
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun May 19 11:41:17 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10119
Summary: Add tuple overload which automatically captures the
names of symbols
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: andrej.mitrovich at gmail.com
--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-05-19 11:41:16 PDT ---
Currently tuple() can capture the state of local variables and wrap them in a
Tuple struct. However you won't have name access:
-----
import std.typecons;
auto foo()
{
int x, y;
return tuple(x, y);
}
void main()
{
assert(foo.x == 0); // fails, no "x"
}
-----
As a workaround you can explicitly name the fields at the call site:
-----
auto foo()
{
int x, y;
return Tuple!(typeof(x), "x", typeof(y), "y")(x, y);
}
-----
But this is tedious and boring, and we can do much better than this. I propose
we introduce either an overload of tuple (if it's possible) or a newly named
function which automatically wraps the arguments by name into a tuple.
Here's an example implementation:
-----
import std.string;
import std.typecons;
auto tuplify(Aliases...)()
{
string gen()
{
string[] lhs;
string[] rhs;
foreach (idx; 0 .. Aliases.length)
{
lhs ~= format("typeof(Aliases[%s])", idx);
lhs ~= format("__traits(identifier, Aliases[%s])", idx);
rhs ~= format("Aliases[%s]", idx);
}
return format("Tuple!(%s)(%s)", lhs.join(", "), rhs.join(", "));
}
return mixin(gen());
}
auto func()
{
int x = 1;
string y = "2";
return tuplify!(x, y);
}
void main()
{
auto res = func;
assert(res.x == 1);
assert(res.y == "2");
}
-----
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list