I encountered an odd bug in a project of mine which is illustrated in the example below:
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, 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?