5 ms·
Rather than #define LIST_FOREACH(list, index, value, code) I'd prefer #define LIST_FOREACH(list, index, value) \ for(index = 0, value = list->items[0]
by alexgartrell 16y ago
Rather than
#define LIST_FOREACH(list, index, value, code)
I'd prefer
#define LIST_FOREACH(list, index, value) \
for(index = 0, value = list->items[0]; \
index < list->size; \
index++, value=list->items[index])
It lets you treat a LIST_FOREACH(...) as a normal for(...) in terms of the other syntax.
edit: I did some looking around and determined that the following is probably BS
[1] If you're wondering why I didn't do
index++, value = list->items[index]
it's because expressions separated by a comma can be evaluated in any order (I believe). My friend ran up against it with a bug in jscore.
- dzorz 16y ago> it's because expressions separated by a comma can be evaluated in any order (I believe). Expressions separated by the comma operator are always evaluated left to right. On the other hand function arguments (which are separated by comma) can be evaluated in any order.
- jpr 16y ago> Expressions separated by the comma operator are always evaluated left to right. That's correct (for C), but I feel the need to add that in function calls the parameters are separated by commas, and they can be evaluated in any order.
- mfukar 16y agoIn the contexts of a function declaration/definition/call, comma is not acting as an operator.
- route66 16y agoYour preference looks a lot like the linux-kernel list.h. (e.g. http://isis.poly.edu/kulesh/stuff/src/klist/ http://isis.poly.edu/kulesh/stuff/src/klist/). Requiring idiosyncratic setup of your list struct though.
- alexgartrell 16y agoIt's certainly inspired by list.h, but I used the same struct as the original. I just think it's crufty to pass "code" as a parameter to a function (in that I've ever really seen it done well). Of course, I'm talking only about C, as I love to overuse lambda in Python like it's my job
- vog 16y agoWhy don't you simply replace index++, value=list->items[index] with value=list->items[++index] ?
- alexgartrell 16y agoI did initially, until I looked up [1] and changed it in the edit. I figure separating it is cleaner. [1] http://en.wikipedia.org/wiki/Comma_operator http://en.wikipedia.org/wiki/Comma_operator
- vog 16y agoBut the combined expression solves exactly your problem! You were not sure whether the increment would always happen before the assignment. In the combined expression you can simply use the pre-increment operator, which will always increment before the rest of the expression. It's shorter, unambiguous and leaves less questions. How can something be any "cleaner" than that? Also, the statements "array[index++]" and "array[++index]" are pretty well-understood idioms in C.
- alexgartrell 16y agoThe following comment adds no value to the discussion, but I'm kind of childish and don't want to look like a dumbass in front of the cool kids :) You'll notice that there's a footnote that isn't actually related to anything in the body of the comment. It's because the footnote was wrong; I just felt like it would be dishonest to hide it in the edit. (this is also the reason the edit: I did some looking around...) portion exists. Interestingly, the edit happened prior to any child posts on the comment, and so was not really a reaction to being corrected. I'd actually googled and found [1], and realized (as someone else said) that function calls don't use the comma as an operator. tl;dr; never, EVER expect left-to-right evaluation of arguments to a function :) [1] http://en.wikipedia.org/wiki/Comma_operator http://en.wikipedia.org/wiki/Comma_operator