Several questions (about inline assembly, ddoc for templates, and foreach)

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat Aug 19 13:00:31 PDT 2006


Marcio wrote:
> Are you sure that works?
> I've just tested the following code with DMD v0.164 (in Linux):
> 
> void main()
> {
>   char[] arr = "test";
> 
>   foreach (inout i, inout c; arr) {
>     if (i == 0) {
>       i++;
>       continue;
>     }
>   }
> }
> 
> And the compiler says: "foreach: key cannot be out".
> Did you use the last version?

Huh.  I could've sworn it used to work that way... but no matter, I just wrote the 
following and it worked fine (DMD 0.164):

# import std.stdio : writefln ;
#
# T[] array (T) (T[] args ...) { return args.dup; }
#
# void main () {
#   int[] arr = array!(int)(1, 2, 0, 3, 4, 5, 0, 6, 7, 8, 0, 9, 10);
#
#   foreach (i, inout x; arr) {
#     if (x == 0) {
#       i++;
#       continue;
#     }
#     writefln("[%2d] %2d", i, x);
#   }
# }

Turns out the 'inout' isn't neccessary anyhow.  This prints, as I expected:
[ 0]  1
[ 1]  2
[ 4]  4
[ 5]  5
[ 8]  7
[ 9]  8
[12] 10


-- Chris Nicholson-Sauls



More information about the Digitalmars-d-learn mailing list