Some examples where explicitly ignoring a value is needed.
foreach (_; 0 .. 10) {}
foo((a, _) => a);
A more advanced implementation might also allow to use _ in assignments not only as declarator.
TypeTuple!(a, b, _) = tup[];
The compiler should disallow to access declarations named _, after deprecation.
The identifier _ should not be checked for variable shadowing to allow nested declarations of _.
If possible the compiler should not generate code to assign/initialize such declarations, unless the operation has a side-effect.
Comment #1 by bearophile_hugs — 2014-09-23T13:09:50Z
I've added my vote.
Once "_" means "ignored" (and it has become a name that can't be read), you can allow code like this where "_" is used more than once:
foreach (_; 0 .. 5)
foreach (_; 0 .. 5) {}
foo((a, _, _) => a);
// some kind of tuple destructuring syntax:
@{a, b, _} = myTup;
Other potentially important usages of the "ignored" value are for pattern matching "ignored" wild-card.
Comment #2 by ketmar — 2014-09-23T17:53:26Z
one of my so-much-universally-hated ;-) patches does exactly this (but for '__' args — two underscores) for foreach args and lambda args (maybe function args too, i don't remember). it's such an amusement to see people requesting same things again and again.
as for 'foreach', this should be allowed too: `foreach (; 0..42)`. i'm using this for some time now and found it very handy.
Comment #3 by hsteoh — 2014-09-26T03:54:13Z
Would this cause interop problems with C programs that use GNU gettext, which conventionally defines '_' to be shorthand for 'gettext'? (Not that I care about such an ugly C hack, but you never know, somebody might care.)
Comment #4 by ketmar — 2014-09-26T04:11:10Z
it shouldn't, but we can use two underscores for that. ids started with two underscores are reserved anyway.
Comment #5 by bearophile_hugs — 2014-10-25T14:59:38Z
There are plans to totally disallow the use of "_" as variable name in Java:
http://openjdk.java.net/jeps/213
>In addition, using underscore ("_") as an identifier, which generates a warning as of Java SE 8, should be turned into an error in Java SE 9.<
Comment #6 by code — 2015-07-08T10:13:11Z
There is precedence for this usage in python, haskell and scala.
Comment #7 by robert.schadek — 2024-12-13T18:28:55Z