www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mysteries of the Underscore

reply Ron Tarrant <rontarrant gmail.com> writes:
I found a mention that in the definition of a delegate, a 
function parameter and its type could be replaced by an 
underscore:

myTestRig.addOnDestroy(delegate void(Widget w) { quitApp(); } );

became:

myTestRig.addOnDestroy(delegate void(_) { quitApp(); } );

I was trying to find some further documentation on this, but I'm 
coming up empty.

Questions:

1) What is this called (substituting an underscore in this 
manner)?

2) Where can a learn more about it?
Dec 24 2018
next sibling parent reply Johan Engelen <j j.nl> writes:
On Monday, 24 December 2018 at 11:18:44 UTC, Ron Tarrant wrote:
 I found a mention that in the definition of a delegate, a 
 function parameter and its type could be replaced by an 
 underscore:

 myTestRig.addOnDestroy(delegate void(Widget w) { quitApp(); } );

 became:

 myTestRig.addOnDestroy(delegate void(_) { quitApp(); } );

 I was trying to find some further documentation on this, but 
 I'm coming up empty.

 Questions:

 1) What is this called (substituting an underscore in this 
 manner)?

 2) Where can a learn more about it?
The underscore is just an identifier but nothing special, it could be any valid identifier like "ldkhfksdkdsg". ``` void ggg(); void takedelegate(void delegate(int) dlg); void foo() { takedelegate( delegate void(asdadasdeg) { ggg(); } ); } ``` The type of the argument is deduced from the function the delegate is passed to. -Johan
Dec 24 2018
parent Ron Tarrant <rontarrant gmail.com> writes:
On Monday, 24 December 2018 at 11:30:31 UTC, Johan Engelen wrote:

 The underscore is just an identifier but nothing special, it 
 could be any valid identifier like "ldkhfksdkdsg".
 -Johan
Thanks, Johan. In a way, I was hoping it was some kind of underscore magic. Now my brain hurts.
Dec 24 2018
prev sibling parent reply bauss <jj_1337 live.dk> writes:
On Monday, 24 December 2018 at 11:18:44 UTC, Ron Tarrant wrote:
 I found a mention that in the definition of a delegate, a 
 function parameter and its type could be replaced by an 
 underscore:

 myTestRig.addOnDestroy(delegate void(Widget w) { quitApp(); } );

 became:

 myTestRig.addOnDestroy(delegate void(_) { quitApp(); } );

 I was trying to find some further documentation on this, but 
 I'm coming up empty.

 Questions:

 1) What is this called (substituting an underscore in this 
 manner)?

 2) Where can a learn more about it?
1) The underscore does nothing and it's just an identifier. Really it just means "unused". It's frequently used in loops to. Ex. foreach (_; 0 .. 100) { // Do something 100 times ... } 2) There is really nothing to learn about it since it's not really thing. However that's not to say there isn't underscore magic in D. Ex. double underscore identifiers are currently reserved (For fields) and shouldn't be used because their behaviors aren't defined in the same way as regular fields. That's however a very different thing and unrelated to this "underscore magic".
Dec 24 2018
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Dec 24, 2018 at 02:45:24PM +0000, bauss via Digitalmars-d-learn wrote:
[...]
 The underscore does nothing and it's just an identifier.
 
 Really it just means "unused". It's frequently used in loops to.
Rather, it's *conventionally* taken to mean "unused". The language actually does not treat it in any special way apart from "normal" identifiers. It's perfectly valid (though probably not recommended!) to declare functions or variables with the name "_" and use them. T -- Knowledge is that area of ignorance that we arrange and classify. -- Ambrose Bierce
Dec 24 2018
parent reply Neia Neutuladh <neia ikeran.org> writes:
On Mon, 24 Dec 2018 08:16:01 -0800, H. S. Teoh wrote:
 Rather, it's *conventionally* taken to mean "unused".  The language
 actually does not treat it in any special way apart from "normal"
 identifiers.  It's perfectly valid (though probably not recommended!) to
 declare functions or variables with the name "_" and use them.
I once, in my callow days, wrote a unittest with variables named _, __, ___, etc. It did not pass code review, but it did amuse.
Dec 24 2018
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Dec 24, 2018 at 04:53:57PM +0000, Neia Neutuladh via
Digitalmars-d-learn wrote:
 On Mon, 24 Dec 2018 08:16:01 -0800, H. S. Teoh wrote:
 Rather, it's *conventionally* taken to mean "unused".  The language
 actually does not treat it in any special way apart from "normal"
 identifiers.  It's perfectly valid (though probably not
 recommended!) to declare functions or variables with the name "_"
 and use them.
I once, in my callow days, wrote a unittest with variables named _, __, ___, etc. It did not pass code review, but it did amuse.
GNU gettext used to (and perhaps still does?) use "_" as the name of the macro to do l10n string lookups. I suppose the idea was brevity for strings which are ubiquitously used, but still. T -- Тише едешь, дальше будешь.
Dec 24 2018
prev sibling parent Ron Tarrant <rontarrant gmail.com> writes:
On Monday, 24 December 2018 at 16:53:57 UTC, Neia Neutuladh wrote:

 I once, in my callow days, wrote a unittest with variables 
 named _, __, ___, etc. It did not pass code review, but it did 
 amuse.
<smirk>
Jan 02 2019