length's type.

Kagamin spam at here.lot
Fri Feb 16 10:01:51 UTC 2024


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;
}
```


More information about the Digitalmars-d-learn mailing list