On Thursday, 8 February 2024 at 05:56:57 UTC, Kevin Bailey wrote:
How many times does the following loop print? I ran into this
twice doing the AoC exercises. It would be nice if it Just
Worked.
```
import std.stdio;
int main()
{
char[] something = ['a', 'b', 'c'];
for (auto i = -1; i < something.length; ++i)
writeln("less than");
return 0;
}
```
Try this:
```
import std.stdio;
int ilength(T)(in T[] a)
{
assert(a.length<=int.max);
return cast(int)a.length;
}
int main()
{
char[] something = ['a', 'b', 'c'];
for (auto i = -1; i < something.ilength; ++i)
writeln("less than");
return 0;
}
```