What's up with staticIota?
Nordlöw via Digitalmars-d
digitalmars-d at puremagic.com
Wed Oct 28 13:43:10 PDT 2015
On Wednesday, 28 October 2015 at 12:09:53 UTC, rcorre wrote:
> Its not uncommon that I need a compile-time list that counts
> from 0..n.
>
> It seems like the only 'standard' way to do this is
> `std.typecons.staticIota`, which is undocumented and has
> package level access.
>
> Looking through the archives I've seen 3 suggestions:
>
> 1. Expose staticIota as-is.
>
> 2. Implement `static foreach` (http://wiki.dlang.org/DIP57),
> allowing loop unrolling over a normal `iota`
>
> 3. `toTypeTuple`
> (https://github.com/D-Programming-Language/phobos/pull/1472),
> which creates a tuple out of a range, so you could use
> `toTypeTuple!iota`.
>
> 4. Everyone implements their own staticIota.
>
> 1 seems like the easiest, though 3 may be the more generally
> useful (and if it were implemented, might make staticIota
> redundant). 2 is nice though it only covers the looping case.
>
> I was just wondering if there is any plan for one of these --
> currently I find myself using `staticIota` from std.typecons,
> relying on the fact that I can access a package-level symbol
> even though I'm not supposed to be able to.
Why doesn't Phobos simply contain an `iota`-overload as
template iota(size_t from, size_t to)
if (from <= to)
{
alias iota = siotaImpl!(to-1, from);
}
private template siotaImpl(size_t to, size_t now)
{
import std.meta: AliasSeq;
static if (now >= to) { alias siotaImpl = AliasSeq!(now); }
else { alias siotaImpl = AliasSeq!(now,
siotaImpl!(to, now+1)); }
}
instead of `staticIota`?
This works for me.
More information about the Digitalmars-d
mailing list