On Thursday, April 21, 2016 04:01:17 PM Bruce Evans wrote: > On Wed, 20 Apr 2016, John Baldwin wrote: > > > On Wednesday, April 20, 2016 01:06:38 PM Bruce Evans wrote: > >> On Wed, 20 Apr 2016, Marcelo Araujo wrote: > >> > >>> 2016-04-20 0:16 GMT+08:00 John Baldwin <j...@freebsd.org>: > >>> > >>>> On Tuesday, April 19, 2016 04:46:13 AM Marcelo Araujo wrote: > >>>>> Author: araujo > >>>>> Date: Tue Apr 19 04:46:13 2016 > >>>>> New Revision: 298247 > >>>>> URL: https://svnweb.freebsd.org/changeset/base/298247 > >>>>> > >>>>> Log: > >>>>> Remove redundant parenthesis. > >>>>> > >>>>> Submitted by: pfg > >>>>> MFC after: 2 weeks. > >> > >> I don't realling like churnging to the nonstandard nitems(). Use > >> of the nonstandard <sys/param.h> is bad enough. > > > > I think it's not that bad from a readability standpoint. Other languages > > have fairly concise syntax for 'for-each' loops and this provides a > > closer variant of that for statically sized arrays. TAILQ_FOREACH() is > > still nicer of course. One could imagine doing some sort of > > ARRAY_FOREACH() that was: > > Ugh, I really realling (sic) don't like the FOREACH macros. The FOREACH > macros add syntactic salt. The queue macros were bad enough before they > had FOREACH. Arrays don't start with such nastiness. > > > #define ARRAY_FOREACH(p, array) \ > > for (size_t __i = 0, (p) = &(array)[0]; __i < nitems((array)); __i++, > > (p)++) > > This only works in the not very usual case of a simple loop from the start > to the end. Even the name EACH becomes wrong if the range is anything else. > For full nastiness, add a few hundred FORFOO macros to handle multi- > dimensional arrays with different access methods, array slices, sentinels > and other terminating conditions, etc.
Actually, combined with break this is a common case. The fairly widespread use of the queue FOREACH macros (and most of the uses of nitems() in the tree) are a testament to this. > > Perhaps better is this: > > > > #define ARRAY_FOREACH(p, array) \ > > for ((p) = &(array)[0]; (p) < &(array)[nitems((array))]; (p)++) > > > > (No need for __i) > > But hiding the indexes forces the old C programming idiom of using > pointers for everything. A mere few hundred FORFOO macros won't do. > For just 1-dimensional FOREACH, you need 3 versions to expose p, i or > both p and i. If someone wanted 'i' they could maintain it on their own through the loop. In practice, 'foreach' is a common idiom in other languages and is used to mean 'for each item in container'. The 'short' syntax always implies a full iteration (though you could use break to bail early). For example, in python: for item in list: # do stuff Or in C++11: for (p : array) { } In C++11 I think this only works for std::array rather than a plain C array, though compared to a fixed-size array for which nitems() works, std::array is equivalent. Both of these work for dynamically sized containers for which our queue macros are the equivalent. -- John Baldwin _______________________________________________ svn-src-all@freebsd.org mailing list https://lists.freebsd.org/mailman/listinfo/svn-src-all To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"