auto bind(alias fun, T)(auto ref T tuple) if (isTuple!T && is(typeof(fun(tuple.expand)))) { return fun(tuple.expand); } unittest { immutable t = tuple(1, 2); auto sum = t.bind!((one, two) => one + two); assert(sum == 3); }
Why not just specify the names when constructing the tuple? -> Sometimes, the tuple is constructed in more than one place. This would require specifying the same names at every construction site. Why not declare the tuple type ahead of time? -> The declaration may need to be placed far away from the construction and use sites, such as in long UFCS chains.
@CyberShadow created dlang/phobos pull request #8372 "[RFC] Fix Issue 22736 - Add destructuring bind for std.typecons.Tuple tuples" fixing this issue: - Fix Issue 22736 - Add destructuring bind for std.typecons.Tuple tuples https://github.com/dlang/phobos/pull/8372
@pbackus created dlang/phobos pull request #8376 "Add std.functional.bind" fixing this issue: - Add std.functional.bind Fixes Issue 22736 - Add destructuring bind for std.typecons.Tuple tuples https://github.com/dlang/phobos/pull/8376
dlang/phobos pull request #8376 "Add std.functional.bind" was merged into master: - 9d1aeeca2b0bcc5cf08f7eebd5283466444112ba by Paul Backus: Add std.functional.bind Fixes Issue 22736 - Add destructuring bind for std.typecons.Tuple tuples https://github.com/dlang/phobos/pull/8376