How templates work (bonus) - Full instantiation of Iota!(1,5)

Simen Kjærås simen.kjaras at gmail.com
Thu Jun 4 12:03:00 UTC 2020


On Thursday, 4 June 2020 at 11:33:48 UTC, Stefan Koch wrote:
> Because it has been done manually there are probably some bugs.
> Can you find them all?

The way you're doing it seems to change about halfway (notice the 
sudden change in slope in the indentation at Seq!(3, ...)), and 
it's not really proper D in any case, but there's a few missing 
exclamation marks (after Iota in Seq!(2, Iota(2 + 1, 5)), and 
after the final Seq in }.Seq => (3, Seq!(4, Seq())))). There's 
also a spurious set of parentheses in }.Seq => (2, (Seq!(3, 
Seq!(4, Seq!())))).

Here's how I would have shown it:

Iota!(1,5):
{
     alias Iota = Seq!(1, Iota!(1 + 1, 5));
     Iota!(1 + 1, 5):
     {
         alias Iota = Seq!(2, Iota!(2 + 1, 5));
         Iota!(2 + 1, 5):
         {
             alias Iota = Seq!(3, Iota!(3 + 1, 5));
             Iota!(3 + 1, 5):
             {
                 alias Iota = Seq!(4, Iota!(4 + 1, 5));
                 Iota!(4 + 1, 5):
                 {
                     alias Iota = Seq!();
                 }.Iota => ()
             }.Iota => (4)
         }.Iota => (3, 4)
     }.Iota => (2, 3, 4)
}.Iota => (1, 2, 3, 4)

(Seq is trivial, so I don't really see a need to expand it every 
step)

Next challenge: Do the divide-and-conquer version.

--
   Simen


More information about the Digitalmars-d mailing list