Comment #0 by snarwin+bugzilla — 2022-02-18T17:42:13Z
As of DMD 2.098.1, the following program fails to compile:
---
struct MustInit
{
string s;
@disable this();
this(string s) { this.s = s; }
}
struct S
{
MustInit member;
this(int n)
{
switch (n)
{
case 1: member = MustInit("one"); break;
case 2: member = MustInit("two"); break;
default: member = MustInit("many"); break;
}
}
}
---
However, the equivalent code using an if-else chain compiles successfully:
---
struct MustInit
{
string s;
@disable this();
this(string s) { this.s = s; }
}
struct S
{
MustInit member;
this(int n)
{
if (n == 1) member = MustInit("one"); else
if (n == 2) member = MustInit("two"); else
member = MustInit("many");
}
}
---
In cases like this one, where the switch statement is equivalent to an if-else chain, the compiler should be able to analyze it the same way.
Comment #1 by robert.schadek — 2024-12-13T19:21:00Z