Issue 13522 - Let's use '_' underscore as official ignore value
Summary: Let's use '_' underscore as official ignore value
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-23 12:29 UTC by Martin Nowak
Modified: 2022-12-17 10:32 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Martin Nowak 2014-09-23 12:29:50 UTC
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 bearophile_hugs 2014-09-23 13:09:50 UTC
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 Ketmar Dark 2014-09-23 17:53:26 UTC
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 hsteoh 2014-09-26 03:54:13 UTC
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 Ketmar Dark 2014-09-26 04:11:10 UTC
it shouldn't, but we can use two underscores for that. ids started with two underscores are reserved anyway.
Comment 5 bearophile_hugs 2014-10-25 14:59:38 UTC
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 Martin Nowak 2015-07-08 10:13:11 UTC
There is precedence for this usage in python, haskell and scala.