Remix.run Logo
teo_zero 2 days ago

> I do kinda like how foreach is implemented right now. This allows me to perform some strict checking for easy loop iteration based bugs and also it kinda looks cool.

Yes but the body in the parameters really is a show stopper. What if the body has a unprotected comma? Like

  VecForeach(&v, e, {
    int x, y;
    ...
  });
Better to expand the macro into one or two "for" and let the body follow. For example:

  #define VecForeachIdx(v, var, idx)\
    for (size idx=0,_d=1; ValidateVec(v),_d; _d--)\
      for (VEC_DATATYPE(v) var={}; idx<(v)->length && (var=VecAt(v,idx),1); idx++)
Please note that I haven't tested it.
servicewelt 2 days ago | parent | next [-]

How would you implement 'var = ...' while respecting that 'VecAt(...)' could return 0? I am currently working on a similar problem and can't seem do seem figure out how to handle these for-loops and setting the iterator properly.

teo_zero 2 days ago | parent [-]

The ",1" ignores var and keeps 1.

brightprogramer 2 days ago | parent | prev [-]

That sounds like a better idea. I'll test it. Debugging with my existing macros is a PITA, but if your suggestion works then it'll work well with debuggers as well! Thanks :)