Mysteries of the Underscore

bauss jj_1337 at live.dk
Mon Dec 24 14:45:24 UTC 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".


More information about the Digitalmars-d-learn mailing list