Bug 9591 – std.typetuple.staticApplyMap

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-25T18:14:31Z
Last change time
2020-03-21T03:56:32Z
Keywords
pull
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-02-25T18:14:31Z
This is an usage example of std.typetuple.staticMap from the online docs: alias T = staticMap!(Unqual, int, const int, immutable int); static assert(is(T == TypeTuple!(int, int, int))); staticMap is similar to map, it maps a given template on a sequence of items. Using Haskell syntax (http://www.haskell.org/haskellwiki/Kind ) it has kind: staticMap :: (* -> *) -> [*] -> [*] In some cases it's also useful a related higher order TypeTuple template like this, kind of the opposite of staticMap: import std.string: leftJustify, center, rightJustify; alias U = staticApplyMap!(string, leftJustify, center, rightJustify); static assert(is(U == TypeTuple!(leftJustify!string, center!string, rightJustify!string))); With kind: staticApplyMap :: * -> [* -> *] -> [*] So the first argument of staticApplyMap is seen as an item, and the following ones as templates that accept an argument. It returns a TypeTuple of all the templates applied on the first given item.
Comment #1 by b2.temp — 2017-09-15T03:45:32Z
std.typetuple doesn't exist anymore and the idea had never been expressed in a pull request.
Comment #2 by schveiguy — 2017-09-15T12:46:35Z
std.typetuple was moved to std.meta, so the request is still valid. We have ApplyLeft and ApplyRight, which take a template as the first parameter, and combined with staticMap, could work as long as the parameters are reversed (the template comes last). Having something that applies a single parameter (or even multipe ones) to a list of templates would be useful as well, and is difficult to do directly. However, I don't think we need a specific function, a helper like this would work: template ApplyWith(Args...) { alias ApplyWith = ApplyLeft!(Args[$-1], Args[0 .. $-1]); } alias T = staticMap!(ApplyLeft!(ApplyWith, string), leftJustify, center, rightJustify); void main() { foreach(f; T) writeln(f("hello", 20)); } It's not perfect. The resulting T isn't a tuple of instantiated templates, it's a tuple of ApplyLeft aliases, awaiting instantiation. But a helper like the above would be useful. Is there an "Instantiate" template that could be used in place of ApplyLeft inside the ApplyWith template? That might be more proper.
Comment #3 by schveiguy — 2017-09-15T12:51:33Z
(In reply to Steven Schveighoffer from comment #2) > Is there an "Instantiate" > template that could be used in place of ApplyLeft inside the ApplyWith > template? That might be more proper. There is, it's just private: https://github.com/dlang/phobos/blob/master/std/meta.d#L1670
Comment #4 by schveiguy — 2017-09-15T13:46:55Z
Comment #5 by schveiguy — 2017-09-17T16:35:49Z
As David Nadlinger pointed out, this can already be solved with Instantiate and ApplyRight. I added a PR to make Instantiate public, and add the solution to this issue as a documented unit test. https://github.com/dlang/phobos/pull/5739
Comment #6 by github-bugzilla — 2017-09-20T09:23:12Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/f3cec8bdd38529848c5c1bed1f5a2a9817e07203 Fix issue 9591 - Allow Instantiate template to be public, give example of how Instantiate + ApplyRight can be used to solve the problem. https://github.com/dlang/phobos/commit/efae08503d9a83d6abdf29270e4c363c393e53de Merge pull request #5739 from schveiguy/fix9591 Fix issue 9591 - Allow Instantiate template to be public
Comment #7 by github-bugzilla — 2017-10-16T09:58:03Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/f3cec8bdd38529848c5c1bed1f5a2a9817e07203 Fix issue 9591 - Allow Instantiate template to be public, give example https://github.com/dlang/phobos/commit/efae08503d9a83d6abdf29270e4c363c393e53de Merge pull request #5739 from schveiguy/fix9591