Bug 8799 – Give example of Tuple mapped to a function

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-10T19:34:00Z
Last change time
2017-01-16T23:24:30Z
Keywords
bootcamp
Assigned to
lucia.mcojocaru
Creator
Jesse.K.Phillips+D
Blocks
16614

Comments

Comment #0 by Jesse.K.Phillips+D — 2012-10-10T19:34:35Z
As per doc comment: http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Tuples After the section starting with "Tuples can be used as arguments to templates, and if so ...", you should mention, that tuples don't expand automatically to multiple function arguments, but their expand property can be used to effect this. Then show an example of this.
Comment #1 by Jesse.K.Phillips+D — 2012-10-19T11:28:57Z
Sorry I miss understood and did not remember that Tuples do auto expand. The actual request is mapping the Tuple to a function, as requested on SO: http://stackoverflow.com/questions/12888263/mapping-variadic-template-arguments-in-d
Comment #2 by andrej.mitrovich — 2014-04-23T10:46:23Z
(In reply to Jesse Phillips from comment #1) > Sorry I miss understood and did not remember that Tuples do auto expand. The > actual request is mapping the Tuple to a function, as requested on SO: > > http://stackoverflow.com/questions/12888263/mapping-variadic-template- > arguments-in-d The following is an updated example. However, the problem is this feature isn't really supported, it just happens to work. There's no telling whether it will break at some point, so I think we shouldn't document it yet. ----- /** Return a Tuple expression of $(D Func) being applied to every tuple argument. */ template Map(alias Func, args...) { static auto ref ArgCall(alias Func, alias arg)() { return Func(arg); } static if (args.length > 1) alias Map = TypeTuple!(ArgCall!(Func, args[0]), Map!(Func, args[1 .. $])); else alias Map = ArgCall!(Func, args[0]); } /// unittest { import std.conv; int square(int arg) { return arg * arg; } int refSquare(ref int arg) { arg *= arg; return arg; } ref int refRetSquare(ref int arg) { arg *= arg; return arg; } void test(int a, int b) { assert(a == 4, a.text); assert(b == 16, b.text); } void testRef(ref int a, ref int b) { assert(a++ == 16, a.text); assert(b++ == 256, b.text); } int a = 2; int b = 4; test(Map!(square, a, b)); test(Map!(refSquare, a, b)); assert(a == 4); assert(b == 16); testRef(Map!(refRetSquare, a, b)); assert(a == 17); assert(b == 257); } -----
Comment #3 by lucia.mcojocaru — 2016-11-01T13:53:24Z
Comment #4 by github-bugzilla — 2016-11-01T20:45:56Z
Comment #5 by github-bugzilla — 2016-12-27T13:11:12Z
Comment #6 by github-bugzilla — 2017-01-07T03:02:29Z
Comment #7 by github-bugzilla — 2017-01-16T23:24:30Z