Bug 9853 – The order of static and shared on module constructors matters when it shouldn't

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-01T18:36:00Z
Last change time
2015-06-09T05:15:21Z
Assigned to
nobody
Creator
issues.dlang

Comments

Comment #0 by issues.dlang — 2013-04-01T18:36:12Z
This code static shared this() { } void main() { } fails to compile, giving this error: q.d(1): Error: constructor q.this constructors are only for class or struct definitions whereas if the static and shared are swapped, it compiles just fine. The order shouldn't matter.
Comment #1 by k.hara.pg — 2013-04-01T19:19:26Z
This is by design. http://dlang.org/class.html#StaticConstructor > The **static** in the static constructor declaration is not an attribute, > it must appear immediately before the this: > > class Foo { > static this() { ... } // a static constructor > static private this() { ... } // not a static constructor > static { > this() { ... } // not a static constructor > } > static: > this() { ... } // not a static constructor > }
Comment #2 by issues.dlang — 2013-04-01T22:27:42Z
Well, that seems incredibly arbitrary, and I have no idea how anyone could claim that static isn't an attribute. It's as much an attribute as const or shared is.
Comment #3 by k.hara.pg — 2013-04-01T22:46:28Z
(In reply to comment #2) > Well, that seems incredibly arbitrary, and I have no idea how anyone could > claim that static isn't an attribute. It's as much an attribute as const or > shared is. I think that the limitation comes from current D parser. Current D parser does not distinguish `static: void foo(){}` and `static void foo(){}`. struct S { pure void foo() {} } is parsed as: struct S { pure { void foo() {} } } There is no "prefixed attribute". But, for static constructor, applying same mechanism is dangerous. struct S { static { this() {} // accidentally makes static constructor. } } I think that is why special rule exists for static constructor. --- Similar problem is in prefixed const and member function. struct S { const int foo() {} // const method foo, returns mutable int const: int bar() {} // const method bar, returns mutable int }