digitalmars.D.learn - Mysteries of the Underscore
- Ron Tarrant (12/12) Dec 24 2018 I found a mention that in the definition of a delegate, a
- Johan Engelen (13/25) Dec 24 2018 The underscore is just an identifier but nothing special, it
- Ron Tarrant (3/6) Dec 24 2018 Thanks, Johan. In a way, I was hoping it was some kind of
- bauss (18/30) Dec 24 2018 1)
- H. S. Teoh (9/12) Dec 24 2018 Rather, it's *conventionally* taken to mean "unused". The language
- Neia Neutuladh (3/7) Dec 24 2018 I once, in my callow days, wrote a unittest with variables named _, __,
- H. S. Teoh (7/16) Dec 24 2018 GNU gettext used to (and perhaps still does?) use "_" as the name of the
-
Ron Tarrant
(2/5)
Jan 02 2019
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
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
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".-JohanThanks, Johan. In a way, I was hoping it was some kind of underscore magic. Now my brain hurts.
Dec 24 2018
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
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
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
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: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 -- Тише едешь, дальше будешь.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
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