Re: Mysteries of the Underscore

2019-01-02 Thread Ron Tarrant via Digitalmars-d-learn

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.






Re: Mysteries of the Underscore

2018-12-24 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 24, 2018 at 04:53:57PM +, 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

-- 
Тише едешь, дальше будешь.


Re: Mysteries of the Underscore

2018-12-24 Thread Neia Neutuladh via Digitalmars-d-learn
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.


Re: Mysteries of the Underscore

2018-12-24 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Dec 24, 2018 at 02:45:24PM +, 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


Re: Mysteries of the Underscore

2018-12-24 Thread bauss via Digitalmars-d-learn

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".


Re: Mysteries of the Underscore

2018-12-24 Thread Ron Tarrant via Digitalmars-d-learn

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.




Re: Mysteries of the Underscore

2018-12-24 Thread Johan Engelen via Digitalmars-d-learn

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



Mysteries of the Underscore

2018-12-24 Thread Ron Tarrant via Digitalmars-d-learn
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?