Bug 4577 – Third way to create a std.typecons.Tuple

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-08-03T15:02:00Z
Last change time
2016-11-15T18:59:36Z
Keywords
bootcamp
Assigned to
andrei
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-08-03T15:02:23Z
Three ways to do something are a lot, but for me it's handy to build a std.typecons.tuple with just field names, and using the type inference to omit their types. With good library helper templates the implementation is simple: import std.typecons: Tuple; import std.string: split; import std.metastrings: Format, toStringNow; template SeriesGen1(string txt, string separator, int max, int min=0) { static if (min > max) const SeriesGen1 = ""; else static if (min == max) const SeriesGen1 = Format!(txt, toStringNow!(max)); else const SeriesGen1 = SeriesGen1!(txt, separator, max-1, min) ~ separator ~ Format!(txt, toStringNow!(max)); } template SeriesGen2(string txt, string separator, int max, int min=0) { static if (min > max) const SeriesGen2 = ""; else static if (min == max) const SeriesGen2 = Format!(txt, toStringNow!(max), toStringNow!(max)); else const SeriesGen2 = SeriesGen2!(txt, separator, max-1, min) ~ separator ~ Format!(txt, toStringNow!(max), toStringNow!(max)); } // not sure about the ref return value // can this be named just "Tuple"? ref auto Tuple_(string names, T...)(T args) if (T.length && split(names).length == T.length) { enum string[] fieldNames = split(names); mixin("return Tuple!(" ~ SeriesGen2!("T[%s], fieldNames[%s]", ", ", T.length-1) ~ ")(" ~ SeriesGen1!("args[%s]", ", ", T.length-1) ~ ");" ); } // demo -------------------- import std.stdio: writeln; ref auto sqr2(int a, float b) { return Tuple_!q{x y}(a * a, b * b); } void main() { ///* with(sqr2(10, 5.5F)) { writeln(x, " ", y); } // test.d(...): Error: sqr2(10,5.5F) is not an lvalue //*/ auto xy = sqr2(10, 5.5F); with(xy) { writeln(x, " ", y); } with(Tuple!(int, "a", float, "b")(10, 5.5F)) { writeln(a, " ", b); } } I'd like this to overload the std.typecons.Tuple, I hope this is possible. I am not sure if this enhancement request deserves a 'patch' keyword tag.
Comment #1 by nick — 2016-11-15T12:04:22Z
This now works: auto entry = tuple!("index", "value")(4, "Hello"); http://dlang.org/phobos/typecons.html#tuple Can we close this?