[NO REPLY] Iterators (rev #2, 2018-1-15)

A place of insane ideas, nothing to see here.
Post Reply
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

[NO REPLY] Iterators (rev #2, 2018-1-15)

Post by Giovanni »

It is easy to get indexes wrong while scanning an array so a new keywork is required: foreach.

Code: Select all

uint32_t array[16];

foreach (p in array) {
  *p = (uint32_t)0
}

foreach (p in "Hello World!!") {
  put(*p);
}


No pointer declaration needed, no need to know the size of the array, no need to use macros for the array size and other tricks, no confusion between 0 and 1 as base and < or <= as relation.

  • Iterating on a scalar type is like iterating on an array of size 1.
  • The pointer is implicitly a pointer to the array element type but constant, it is not modifiable.
  • Element index can be obtained using pointer arithmetic (p - array).

Giovanni
Post Reply