3 ms·
Correct, _ is a pattern that does not bind to any name. While the implications of that may not be simple, the description certainly is. Names that start with u
by steveklabnik 2y ago
Correct, _ is a pattern that does not bind to any name. While the implications of that may not be simple, the description certainly is.
Names that start with underscores are still names, and work like any other name. The “unused variable” into can be suppressed with a leading underscore like any other name, but that’s purely about the lint and not semantics.
- gpderetta 2y agoSo the issue really is that the match-and-discard wildcard uses a spelling that would otherwise be a valid identifier name causing an ambiguity to the reader, if not to the compiler. In retrospect using a different character (say '*') would have been better, clueing the reader that something different was happening; but I guess there is long history of this use of _ in ML languages.
- steveklabnik 2y agoI mean, it’s never a valid identifier, and that is spelled out in the reference too.
- gpderetta 2y agoof course it isn't, it acts as a keyword, but it looks like a valid identifier. As this difference doesn't matter 99% of the time, it can be be easily overlooked, becoming a pitfall the 1% that matters. Of course it too late to change it, but pointing it is a must. I understand rust has a linter pass specifically to catch this sort of issues.
- kazinator 2y agoI independently came up with a similar hack in TXR Lisp. When a variable is named by a gensym, unused warnings are suppressed. Gensyms are almost always symbols generated by macros. Machine generated code often has cases where variables are unused. This design decision is a trade off. Any situation in which a variable is unused is potentially a bug, even if the variable is in code written by a macro and named by a generated symbol. But it is a burden on macro writers to ensure that all possible cases of general code are free of unused variable warnings.