Bug 24334 – parameter name is ignored in invocation of struct constructor with default values
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-01-12T01:21:12Z
Last change time
2024-01-13T12:47:59Z
Assigned to
No Owner
Creator
Jim Balter
Comments
Comment #0 by jim — 2024-01-12T01:21:12Z
struct Foo
{
this(int a, int b = 2, int c = 3)
{
imported!"std.stdio".writefln!"a=%s b=%s c=%s"(a, b, c);
}
}
void main()
{
auto foo = Foo(1, c: 99);
}
Expected output: a=1 b=2 c=99
Actual output: a=1 b=99 c=3
(This bug is dangerous, and quite shocking for a mature language.)
Note that the problem is only with a constructor. e.g, this works correctly:
struct Bar
{
static void bar(int a, int b = 2, int c = 3)
{
imported!"std.stdio".writefln!"a=%s b=%s c=%s"(a, b, c);
}
}
void main()
{
Bar.bar(1, c: 99);
}
output: a=1 b=2 c=99
Comment #1 by jim — 2024-01-12T01:30:55Z
P.S. The default constructor works correctly:
struct Foo
{
int a, b = 2, c = 3;
}
void main()
{
auto foo = Foo(1, c: 99);
imported!"std.stdio".writefln!"a=%s b=%s c=%s"(foo.a, foo.b, foo.c);
}
output: a=1 b=2 c=99
Comment #2 by nick — 2024-01-12T12:22:06Z
> and quite shocking for a mature language
Named parameters are not mature, support began to be added in the April release last year. They were not mentioned in any changelogs and should have been behind a preview switch*. But the spec was updated, it should perhaps have a warning about stability.
* https://forum.dlang.org/post/[email protected]
Comment #3 by dkorpel — 2024-01-12T13:50:21Z
This has been fixed by https://github.com/dlang/dmd/pull/15662
The output I get with dmd master is:
```
a=1 b=2 c=99
```
Which is your expected output. Please update your compiler version.
Comment #4 by nick — 2024-01-12T20:54:08Z
2.103.1 to 2.104.2: Success with output: a=1 b=99 c=3
Since 2.105.3: Success with output: a=1 b=2 c=99
Comment #5 by jim — 2024-01-12T22:06:31Z
> it should perhaps have a warning about stability
Or perhaps the compiler test suite should cover basic aspects of the feature.
Comment #6 by nick — 2024-01-13T12:47:59Z
> Or perhaps the compiler test suite should cover basic aspects of the feature.
It did by the time the spec mentioned named arguments.