Function Parameters without Names?

Ali Çehreli acehreli at yahoo.com
Sun Feb 20 01:26:39 UTC 2022


On 2/19/22 15:37, Vijay Nayar wrote:

 > The problem is that the parameter of the constructor actually has no
 > name at all. Thus, the statement `this.val = val` simply sets the member
 > variable to its own value

Wow! I've never come accross that one before. :)

 > What is the main motivator to allow parameters with no names?

This is common in C++ as well. You have to have a parameter because an 
interface requires it but you don't really have any use for that 
parameter. If you name it, then there may be "unused variable" warning. 
So, it is common to comment out the parameter name so that it is 
instructive to people who look at the code:

void foo(int /* length */) {
   // ...
}

 > Do they get an automatic implied name like `_` or something?

The function is compiled to take the specified type of a parameter but 
it is simply not used. (Names are only for source code so it doesn't 
matter.)

Ali



More information about the Digitalmars-d-learn mailing list