August 03, 2010 [Issue 4577] New: Third way to create a std.typecons.Tuple | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=4577 Summary: Third way to create a std.typecons.Tuple Product: D Version: D2 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-08-03 15:02:23 PDT --- 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
October 07, 2010 [Issue 4577] Third way to create a std.typecons.Tuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile_hugs@eml.cc | http://d.puremagic.com/issues/show_bug.cgi?id=4577 Andrei Alexandrescu <andrei@metalanguage.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |ASSIGNED CC| |andrei@metalanguage.com AssignedTo|nobody@puremagic.com |andrei@metalanguage.com -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation