[Issue 4085] Steps toward a static foreach

2017-07-10 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=4085

Vladimir Panteleev  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 Resolution|--- |MOVED

--- Comment #13 from Vladimir Panteleev  ---
https://github.com/dlang/dmd/pull/6760

Closing in favor of DIP 1010:
https://github.com/dlang/DIPs/blob/master/DIPs/DIP1010.md

--


[Issue 4085] Steps toward a static foreach

2017-03-20 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=4085

Daniel Čejchan  changed:

   What|Removed |Added

 CC||czda...@gmail.com

--


[Issue 4085] Steps toward a static foreach

2015-06-09 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=4085

Andrei Alexandrescu and...@erdani.com changed:

   What|Removed |Added

Version|future  |D2

--


[Issue 4085] Steps toward a static foreach

2015-06-05 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=4085

naptime naptimeentertainm...@gmail.com changed:

   What|Removed |Added

 CC||naptimeentertainment@gmail.
   ||com

--


[Issue 4085] Steps toward a static foreach

2014-03-10 Thread d-bugmail
https://d.puremagic.com/issues/show_bug.cgi?id=4085



--- Comment #12 from bearophile_h...@eml.cc 2014-03-10 01:22:14 PDT ---
This suggests the introduction of a static foreach, so the current ER is not
needed:
http://wiki.dlang.org/DIP57

So I think the static foreach part of this proposal could be considered closed
and replaced by the DIP.

Something like Iota is still needed because static foreach doesn't replace
fully the foreach on tuples and you static foreach(0 .. n) can't use a step
value anyway.

On the other hand in the pull requests staticIota was replaces by a toTypeTuple
(https://github.com/D-Programming-Language/phobos/pull/1472  but I don't know
its current static) so eventually this whole issue could be closed.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2013-08-22 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085



--- Comment #10 from bearophile_h...@eml.cc 2013-08-22 16:50:01 PDT ---
Adapted from two comments by Timon Gehr:

 It's important to keep them separate regardless. static foreach is close 
 to useless if it introduces a new scope for its body.
 
 I.e.:
 
 int main() {
 foreach (_; Seq!int) {
 int x;
 }
 return x; // error
 }
 
 int main() {
 static foreach (_; Seq!int) {
 int x;
 }
 return x; // ok
 }
 
 I think the relationship between foreach and static foreach 
 should essentially mirror that of if and static if.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2013-08-22 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085



--- Comment #11 from bearophile_h...@eml.cc 2013-08-22 16:53:06 PDT ---
Another comment by Timon Gehr:

 We really need to define a consistent semantics for compile 
 time symbol manipulation though.

 Eg:

 class C{
 int a;
 static foreach(x;__traits(allMembers,C)){
 mixin(int ~__traits(identifier,x)~b;);
 }
 }

 What is this supposed to do? class C{ int a,ab; }? 
 Non-terminating compilation? Error?

 My best guess is that the above code should be illegal,

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2013-07-29 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085



--- Comment #9 from monarchdo...@gmail.com 2013-07-29 09:16:44 PDT ---
https://github.com/D-Programming-Language/phobos/pull/1440

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2013-07-26 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085



--- Comment #8 from monarchdo...@gmail.com 2013-07-26 00:27:42 PDT ---
(In reply to comment #7)
 (In reply to comment #4)
 
  Copy pasting a proposed implementation from 10712. Pretty much the same 
  thing,
  but also handles indiscriminate types. It passes the prior tests, as well as
  handles the useage with doubles, or chars:
 
 Just a reminder: elsewhere I asked for an improvement to iota(), to let it
 optionally accept a string (like std.random.uniform) that allows to generate
 complete intervals of a type or handy closed intervals:
 
 iota![]('a', 'z')
 iota![](cast(ubyte)0, cast(ubyte)255)

http://d.puremagic.com/issues/show_bug.cgi?id=10466

I've taken note. I think it is a good idea, but it might be difficult to
implement what with all the overloads.

 Currently I think it's impossible to generate the full range of a type like
 ubyes with iota, and it's not handy to generate the complete range of 
 lowercase
 char letters, you need to use iota('a', '{'), that currently doesn't work for
 other reasons.

http://d.puremagic.com/issues/show_bug.cgi?id=6447
http://d.puremagic.com/issues/show_bug.cgi?id=9447

Fixing the issue of iota and non built-in integral is on my todo list.

BTW, you could also write it as (provided char is accepted at all):
iota('a', cast(char)('f' + 1))
or
iota!(char,char)('a', 'f'+1);

But in both cases, integral promotion kind of gets in the way of clean code
:/

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2013-07-25 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085


monarchdo...@gmail.com changed:

   What|Removed |Added

 CC||temta...@gmail.com


--- Comment #3 from monarchdo...@gmail.com 2013-07-25 04:11:03 PDT ---
*** Issue 10712 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2013-07-25 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085


monarchdo...@gmail.com changed:

   What|Removed |Added

 CC||monarchdo...@gmail.com


--- Comment #4 from monarchdo...@gmail.com 2013-07-25 04:20:03 PDT ---
(In reply to comment #3)
 *** Issue 10712 has been marked as a duplicate of this issue. ***

Copy pasting a proposed implementation from 10712. Pretty much the same thing,
but also handles indiscriminate types. It passes the prior tests, as well as
handles the useage with doubles, or chars:

//
template Iota(alias h)
{
alias Iota = IotaImpl!(0, h, 1);
}
template Iota(alias l, alias h)
{
alias Iota = IotaImpl!(l, h, 1);
}
template Iota(alias l, alias h, alias inc)
{
alias Iota = IotaImpl!(l, h, inc);
}

template IotaImpl(alias l, alias h, alias inc, T...)
{
alias E = CommonType!(l, h, inc);
static if (inc == 0)
static assert(0, increment must be non-0);
else static if (inc  0  l = h)
alias IotaImpl = T;
else static if(inc  0  l = h)
alias IotaImpl = T;
else
alias IotaImpl = IotaImpl!(cast(E)(l + inc), h, inc, T, cast(E)l);
}
//

//
foreach(idx; Iota!(0, 0))
write(idx, ' '); // prints 
writeln();

foreach(idx; Iota!(0, 10))
write(idx, ' '); // prints 0, 1, ..., 9
writeln();

foreach(idx; Iota!(2, 10))
write(idx, ' '); // prints 2, 3, ..., 9
writeln();

foreach(idx; Iota!(0, -10, -1))
write(idx, ' '); // prints 0, -1, ..., -9
writeln();

foreach_reverse(idx; Iota!(-9, 1))
write(idx, ' '); // prints 0, -1, ..., -9
writeln();

foreach(idx; Iota!(0.5, 10))
write(idx, ' '); // prints 0.5, 1.5, ..., 9.5
writeln();

foreach(idx; Iota!(0, 1, 0.1))
write(idx, ' '); // prints 0 0.1 ... 0.9
writeln();

foreach(idx; Iota!('a', cast(char)('z' + 1), cast(char)1))
write(idx, ' '); // prints a b ... z
writeln();
//

Maybe it's time to submit this to phobos?

This belongs in typecons?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2013-07-25 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085



--- Comment #5 from Temtaime temta...@gmail.com 2013-07-25 04:28:53 PDT ---
I'm agree with Monarchdodra.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2013-07-25 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085



--- Comment #6 from bearophile_h...@eml.cc 2013-07-25 04:53:45 PDT ---
(In reply to comment #4)

 Copy pasting a proposed implementation from 10712. Pretty much the same thing,
 but also handles indiscriminate types. It passes the prior tests, as well as
 handles the useage with doubles, or chars:

Good (despite in my code I avoid using std.range.iota() with floating point
numbers, because to me it seems a bit bug-prone).


 Maybe it's time to submit this to phobos?

I prefer a static foreach in the D language, even a very simple one (example:
you can't use a foreach(Iota) at module scope). But in the meantime it's handy
to have a Iota() in Phobos.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2011-06-01 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085



--- Comment #2 from bearophile_h...@eml.cc 2011-06-01 14:50:48 PDT ---
Static foreach loops have a small disadvantage. This is the original code:

const int sum1 = foo!10() + foo!15() + foo!80();


The same using a static foreach:

int sum2;
foreach (x; TypeTuple!(10, 15, 80))
  sum2 += foo!x();


The small disadvantage is that now x can't be const.
In theory a smarter type system is able to see sum2 too is const.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4085] Steps toward a static foreach

2010-06-06 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4085



--- Comment #1 from bearophile_h...@eml.cc 2010-06-06 07:09:44 PDT ---
// Until bug 4085 gets fixed, I use something similar to this.
// I suggest to not add this to Phobos because it
//   will become useless when bug 4085 is partially of fully fixed.



import std.typetuple: TypeTuple;
import std.traits: isArray;

version(unittest) {
import std.string: split; // for arrayToTypeTuple!() unittests
}

/
Converts at compile time an enum array into a TypeTuple that
contains the same items.

Example:

foreach (i; arrayToTypeTuple!([1, 2]))
static assert(i  0);

*/
template arrayToTypeTuple(alias items) {
static assert (isArray!(typeof(items)));
static if (items == null || items.length == 0) // bug 4284
alias TypeTuple!() arrayToTypeTuple;
else
alias TypeTuple!(items[0], arrayToTypeTuple!(items[1..$]))
arrayToTypeTuple;
}

unittest { // Tests of arrayToTypeTuple!()
string[] items;
foreach (s; arrayToTypeTuple!(split())) {
static assert(s.length  0);
items ~= s;
}
assert(items.length == 0);

items.length = 0;
foreach (s; arrayToTypeTuple!(split(foo))) {
static assert(s.length  0);
items ~= s;
}
assert(items == [foo]);

items.length = 0;
foreach (s; arrayToTypeTuple!(split(foo bar red))) {
static assert(s.length  0);
items ~= s;
}
assert(items == [foo, bar, red]);

foreach (i; arrayToTypeTuple!([1, 2]))
static assert(i  0);
} // End tests of arrayToTypeTuple!()



/
Template, similar to iota(), but generates a tuple at compile time.

Useful for static foreach loops, where range extrema are compile time
constants:
---
foreach (i; Iota!(3))
a[i] = b[i];

// becomes unrolled and compiled as:
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
---
*/
template Iota(int stop) {
static if (stop = 0)
alias TypeTuple!() Iota;
else
alias TypeTuple!(Iota!(stop-1), stop-1) Iota;
}

/// ditto
template Iota(int start, int stop) {
static if (stop = start)
alias TypeTuple!() Iota;
else
alias TypeTuple!(Iota!(start, stop-1), stop-1) Iota;
}

/// ditto
template Iota(int start, int stop, int step) {
static assert(step != 0, Iota: step must be != 0);

static if (step  0) {
static if (stop = start)
alias TypeTuple!() Iota;
else
alias TypeTuple!(Iota!(start, stop-step, step), stop-step) Iota;
} else {
static if (stop = start)
alias TypeTuple!() Iota;
else
alias TypeTuple!(Iota!(start, stop-step, step), stop-step) Iota;
}
} // End Iota!(a,b,c)

unittest { // Tests of Iota!()
static assert(Iota!(0).length == 0);

int[] a;

foreach (n; Iota!(5))
a ~= n;
assert(a == [0, 1, 2, 3, 4]);

a.length = 0;
foreach (n; Iota!(-5))
a ~= n;
assert(a == new int[0]);

a.length = 0;
foreach (n; Iota!(4, 7))
a ~= n;
assert(a == [4, 5, 6]);

a.length = 0;
foreach (n; Iota!(-1, 4))
a ~= n;
static assert(Iota!(-1, 4).length == 5);
assert(a == [-1, 0, 1, 2, 3]);

a.length = 0;
foreach (n; Iota!(4, 2))
a ~= n;
assert(a == new int[0]);

a.length = 0;
foreach (n; Iota!(0, 10, 2))
a ~= n;
assert(a == [0, 2, 4, 6, 8]);

a.length = 0;
foreach (n; Iota!(3, 15, 3))
a ~= n;
assert(a == [3, 6, 9, 12]);

a.length = 0;
foreach (n; Iota!(15, 3, 1))
a ~= n;
assert(a == new int[0]);

a.length = 0;
foreach (n; Iota!(10, 0, -1))
a ~= n;
assert(a == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);

a.length = 0;
foreach (n; Iota!(15, 3, -2))
a ~= n;
assert(a == [15, 13, 11, 9, 7, 5]);

static assert(!is(typeof( Iota!(15, 3, 0) ))); // stride = 0 statically
asserts
} // End tests of Iota!()


void main() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---