www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What's up with staticIota?

reply rcorre <ryan rcorre.net> writes:
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.
Oct 28 2015
next sibling parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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.
Oct 28 2015
prev sibling parent qsdf <qsdf qsdf.qsdf.qsdf.fi> writes:
On Wednesday, 28 October 2015 at 12:09:53 UTC, rcorre wrote:
 [...]
 1. Expose staticIota as-is.
 2. Implement `static foreach`
 3. `toTypeTuple` [...]
1. It "just" requires to change visibility + ddoc. Until maybe one day 2.
Oct 28 2015