3 ms·
> but is that a useful feature? It would solve the usability issues experienced by Simon Tatham, described in his article. For instance, if one of the clauses
by kazinator 3y ago
> but is that a useful feature?
It would solve the usability issues experienced by Simon Tatham, described in his article. For instance, if one of the clauses has x->foo, it wouldn't matter that the actual x argument in a given instance is char *, that cannot be dereferenced as x->foo.
It would be up to the programmer to test every one of the supported cases of their _Generic construct.
One way to avoid problems would be to include only cases that are used in your codebase. Before adding a new case, add a use* of your generic macro with the currently unsupported type. That should fail to build. Then, add the case. (TDD style).
The preprocessor's #ifdef allow syntactically invalid code. This is very useful; it lets you have conditional sections of code that are understood only by certain compilers due to using their extensions.
Arguments to preprocessor macros, if not planted into syntax, can be syntactic gibberish, but parentheses have to balance. You cannot invoke a macro as mac({(}) and such.
_Generic is meant to work with macros; it should have some macro-like forgiveness in it to make it as useful as possible.
In Common Lisp, the #+ and #- syntax for conditionally skipping forms is similarly defined as doing a minimal parsing for balancing parentheses and a few other details. This allows #+ to switch between implementation-specific code that includes implementation-specific read syntax that cannot be read by all implementations.
http://www.lispworks.com/documentation/lw51/CLHS/Body/02_dhq.htm http://www.lispworks.com/documentation/lw51/CLHS/Body/02_dhq...
It works by binding *read-suppress* to true and then calling read to read the form.
- Dylan16807 3y ago> It would solve the usability issues experienced by Simon Tatham, described in his article. In a very overkill way, though. You can solve the issue in the article by parsing the expression but not doing anything with it. You don't need to allow invalid syntax for that problem. So to be clear, I'm asking specifically about whether it's useful to allow invalid syntax inside _Generic. What does that allow in practical use, moreso than allowing valid syntax with invalid types/semantics?
- kazinator 3y agoIt's easy to implement and obviously correct. I can't think of a situation in which it would not work, or on which it would be a nuisance (prevent the programmer from doing something sensible). I don't know that about the implementation which parses the dead code. Implementing it could be troublesome in some way. In any compiler that checks constraints while parsing, implementing parsing without constraint checking means going to all those places and checking a flag. Actually, it occurs to me that none of this is necessary. How the construct can work is by pretending that the argument in the dead cases has the type of the left side of the association list entry: So if we have _Generic(x, char * : puts(x), dev * : puts(x->devname)); which is invoked with x being "abc", the dead dev * clause should be parsed and type checked under the pretense that there is a dev *x declaration in scope. And not that x is of type char *. The implementation should be, anyway, generating a hidden local variable (gensym) to take the value of the expression. We can imagine a code transformation like this (using GCC brace syntax): _Generic(x, char * : ({ char *__g0025 = x; puts(__g0025); }), dev * : (({ dev *__g0025 = x; puts(__g0025->devname); })) Now here, we just have to suppress type checking of the dev *__g0025 = x; declaration where the only problem is that x is incompatible with __g0025. (That declaration wouldn't even have to be source code: it can be injected at the AST level.) While this is all cool, my bracket counting idea is much simpler and more robust toward cases like different syntactic extensions being used from different compilers or other unforseen snags.