D-Programming-Language/phobos — pull request #2013 by monarchdodra (2014-03-16 16:19:10+00:00)
Title: improve adjoin

This is the last change I had planned in adjoin.

First, it provides "length == 1" and "length > 1" overloads as asked for by @AndrejMitrovic in #2008. It changes indentation, so I highly recommend viewing without takes whites into account ( https://github.com/D-Programming-Language/phobos/pull/2013/files?w=1 )

Second, it uses a mixin, to "in-place" build the returned tuple. This makes both the implementation and resulting code *much* simpler, and removes the need to emplace. It is also generally more correct, since the old code never actually initialized any padding bits. Furthermore, it allows better and safer constructions of "hard to build" types (const types, for instance).

--------

Thoughts:

1. I added a hand written case for `length == 3`, because it's 2 lines of code, and reduces dependencies. Also, it "would" be required to implement a trivial "recursive" implementation, so I think it is worth typing it.

2. The mixin could have been replaced by `return tuple(adjoin!(F[0 .. $/2])(a).expand, F[$/2 .. $])(a).expand)`. However, I think it's more complicated, and could cause extra un-needed runtime postblits, or generally more overhead in a non-optimized/inline build.

3. I could remove the `range` dependency with a local function, but I think the 1-liner is cleaner than rolling out a lambda with a foreach that writes into a string.