Function Parameters without Names?

Vijay Nayar madric at gmail.com
Sat Feb 19 23:37:01 UTC 2022


I encountered an odd bug in a project of mine which is 
illustrated in the example below:

```d
class Thing {
   int val;
   this(int) {
     this.val = val;
   }
}

void main()
{
   auto t = new Thing(3);
   assert(t.val != 3);
}
```

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, thus it stays the same as 
`int.init`.

According to the 
[specification](https://dlang.org/spec/function.html), this is 
permissible via the following grammar rules: `FuncDeclaration` => 
`FuncDeclarator` => `FuncDeclaratorSuffix` => `Parameters` => 
`ParameterList` => `Parameter` => `Declarator` => 
`ParameterAttributes_opt Type`.

What is the main motivator to allow parameters with no names? Do 
they get an automatic implied name like `_` or something?


More information about the Digitalmars-d-learn mailing list