5 ms·
> suggestion that calling a macro "select" with the obvious complete breakage under unix would be fine. There are lots of API's in the world; you can't ponder
by kazinator 8y ago
> suggestion that calling a macro "select" with the obvious complete breakage under unix would be fine.
There are lots of API's in the world; you can't ponder about all of them when naming a macro. How do you know the next system vendor doesn't have a choose function? Or if not the next system vendor, then some third party library or module you'd like to use or whatever.
POSIX doesn't reserve the select identifier for use as a macro, unless the header file is included which defines it. It is not "complete breakage", and there are workarounds in that situation, one possibility being:
#undef select
#define whatever mill_choose_init___
The breakage only happens for code that uses either the select macro or the select function, if the headers are included in the proper order: the system headers first, then libmill.h. I.e. this by itself doesn't cause a problem (except in the improble case that <sys/select.h> defines a select macro):
#include <sys/select.h>
// #undef select at worst!
#include "libmill.h" // with #define select macro
This does cause a problem, 100%:
#include "libmill.h" // with #define select macro
#include <sys/select.h>
But you should never do that: do not include local headers before system or third-party headers, unless you're sure they don't define any macros.
There is also no problem if the rest of the file uses the select macro. The only issue arises if the rest of the file wants to use the select function, and then we put in a workaround. You're not going to call the select function in hundreds of places, where you also need the libmill header.
TL;DR: it's not anywhere near a "nightmare".
Moreover, libmill defines a few macro with short names. Conceivably #define end could be a problem, as well as #define in(..) and #define out(...).
For instance if we have a "range.h" which has this:
struct range {
int beg;
int end;
};
and we do this:
#include "libmill.h"
#include "range.h"
then libmill's end macro will screw up the struct range declaration, not to mention any place where we use end as an identifier. It's not an uncommon local variable name, like for pointers pointing to the end of a memory range.
- kevin_thibedeau 8y agoThe stdlib namespace is golden. The POSIX namespace is silver. Only the foolhardy tread on either. One might even want to use libmill in conjunction with some socket code to build a non-blocking state machine using select().
- kazinator 8y agoI would say that such work would be a candidate for rolling into libmill itself, so the application wouldn't deal with select at all. Note that the code already has some multiplexing gadgetry there, switchable between Linux's epoll, BSD's kqueue and the portable poll (see epoll.inc, kqueue.inc and poll.inc).
- CoolGuySteve 8y ago"Speaking as a professional", if you wrote something like this in my codebase with this justification, I'd fire you before any more carnage could be done. Holy shit. Standard practice in C is to prefix your functions with their 'namespace' to avoid collisions. In this case, I don't even like that they used "choose" instead of "mill_choose" or "mill_select". But redefining select() is a whole other level of inconsiderate.
- kazinator 8y agoI'm working within the premise of this library, which is to provides un-hygienic syntactic extensions to C via preprocessor macros, which are presumably supposed to look reasonably good in comparison to Go. Did you notice the #define end, which is a common name for struct members and local variables. Redefining select is certainly not a whole other level of inconsiderate in that light. The caveat is that whoever uses this should include it last, after any other headers, and then deal with the macro clashes: don't use "choose" or "end" in their code and so on. One way to mitigate this is to have these macros in a separate header, say "libmill-macros.h", so that the rest of the API is available without them.
- stingraycharles 8y agoYou’re suggesting creating incredibly fragile code that will cause a lot of cognitive overload which is completely unnecessary in the first place. Don’t do this, ever. Just because it’s technically possible doesn’t mean it’s a good idea.
- kazinator 8y agoI have no idea what you're talking about; we're discussing small, incremental changes in something whose design I have nothing to do with otherwise.
- jacoblambda 8y agothey are suggesting improvements. The code as a whole is problematic due to a number of reasons and they are offering improvements. If they had prefaced everything with add the namespace to the definitions, I doubt most people would have had as much of a problem with it. From what it seems, they didn't though because the developers don't seem to be worried about library namespace collisions.
- jrockway 8y agoThere are lots of APIs in the world, that's true. The one with "select" in it is a staple of nearly every C program and has existed for decades. I think it would be wise to name your stuff so as not to collide with it. If someone someday makes a name that collides with yours, that's a problem for that "someday". Avoiding today's problem today seems pretty valuable.
- kazinator 8y agoThanks, I am quite well informed in this sort of debate. POSIX systems in fact go out of their way to support conforming ISO C programs, which are permitted to contain things like: void select(int x) // not in any ISO C reserved namespace! { selection = x; } In the case of the GNU C library how it works is that select is a weak symbol aliasing for __select. If anything in the library needs to use select, it calls __select, so as not to be derailed by a user program that redefines the weak symbol. Issues with macros are less severe compared to clashes in actual external linkage. No weak symbol support needed in shared libs; just something has to be #undef-ed here, or an order of headers rearranged there.
- deleted 8y ago[deleted]
- tempodox 8y agoI agree that the “end }” syntax is bad. I disagree with your opinion that `choose` “should” be named `select`. To software written in C, the syntax or naming choices of other languages are really quite irrelevant. The code must be workable in C (and pertaining context, of which POSIX is a part), with minimum fuss and, as far as at all possible, without necessitating any “workarounds”, however minor you opine they would be.
- kazinator 8y agoIt is not realistic to demand there be possibility of name clashes in the use of a library that uses non-hygienic macros with short names to simulate the syntax of another language. > To software written in C, the syntax or naming choices of other languages are really quite irrelevant. However, at least one person out there thinks that the syntax and naming choices of Go are relevant to software written in C, so they wrote this thing called "libmill". Oh wait ... what were we talking about?