3 ms·
I would have designed the feature like this: _Generic(<expr>, type1 : ( expr1 ), type2 : ( expr2 ), ... default : ( expr )) Here, the parentheses shown in t
by kazinator 3y ago
I would have designed the feature like this:
_Generic(<expr>, type1 : ( expr1 ), type2 : ( expr2 ), ... default : ( expr ))
Here, the parentheses shown in this phrase pattern are required.
The implementation would only parse and semantically analyze the expression of the matching type. For the others, the ( expr ) would be treated as a token sequence to be skipped, which has to contain valid tokens, and balancing parentheses, square brackets and braces.
E.g.
char *p = "foo"
_Generic(p : int : ("foo" ++ / ? ([]&xyz) { ; } ),
char * : (p[0]))
Here, the type of p doesn't match int, and so the interior token sequence of ("foo" ++ / ? ([]&xyz) { ; } ) would just be scanned to check for balancing parentheses, brackets and braces, which allows the parser to locate the next clause in the association list.
- Dylan16807 3y agoThat would allow you to put syntactically invalid code into the expressions, but is that a useful feature? It's not very hard to have valid syntax in a situation like this. The main issue here is type enforcement.
- 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.
- reichstein 3y agoJust admit that it's pattern matching, and introduce a new variable for each match: ``` _Generic(expr0, type1 id1?: expr1, ... typeN idN?: exprN, default id0?: exprDefault ) ``` Every identifier is optional, but if you include `idX`, it gets bound to the value of `expr0` with type `typeX` in the following expression, or with the type of `expr0` for the default case. Then you can safely refer to the value at its now-known type, even if `expr0` was not a variable to begin with.