* structs always have constructors. So this snippet is wrong:
> struct S
> {
> immutable int c = 123; // This should be static, compiler issues error
> // No constructor
> }
In fact, S has constructor. All structs have constructor with
parameter count that matches member field count. And judging only by S
definition you can't ever possibly say if "c" will be the same in all
S instances or not.
You're talking about a struct static initializer. That is not the same as a constructor.
> Second, it is consistent because it stays the same both for
struct members and local variables. This is much more important than
prohibiting something based on use case approach.
It is inconsistent with module variables.
const int x = 7;
static this()
{
x = 5;
}
You cannot change the value of a local-scope const variable, once it has been initialized.
You cannot change the value of a module-scope const variable, once it has been initialized. Not even in a constructor.
Up to now:
You cannot change the value of a struct member const variable, once it has been initialized. Not even in a constructor.
I think the consistency argument acts AGAINST this new behaviour.
I think that the reason that having both an initializer and a constructor appears desirable, is actually as a workaround for the lack of struct default constructors.