Bug 7066 – You can redefine .init and .stringof without error

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-12-04T15:44:59Z
Last change time
2024-12-13T17:57:10Z
Keywords
spec
Assigned to
No Owner
Creator
Peter Alexander
Blocks
12545, 12233
See also
https://issues.dlang.org/show_bug.cgi?id=12545, https://issues.dlang.org/show_bug.cgi?id=12233, https://issues.dlang.org/show_bug.cgi?id=14237
Moved to GitHub: dmd#18386 →

Comments

Comment #0 by peter.alexander.au — 2011-12-04T15:44:59Z
The .init and .stringof properties of types can be redefined. A lot of code depends on the definition of .init, so being able to change this breaks a lot of Phobos. class Foo { static immutable int init; static immutable int stringof; } int x = Foo.init; int y = Foo.stringof; DMD should give an error just like it does for .sizeof, .alignof, and .mangleof
Comment #1 by bugzilla — 2012-01-21T01:11:11Z
They're actually supposed to be overridable at the moment. I had thought there might be a use for this, but so far none have materialized. But to change it is an enhancement request.
Comment #2 by Jesse.K.Phillips+D — 2014-02-24T22:00:11Z
How about it is disable until a compelling argument is provided. Or if someone complains during a beta.
Comment #3 by andrej.mitrovich — 2014-04-21T19:45:18Z
*** Issue 8817 has been marked as a duplicate of this issue. ***
Comment #4 by davidsp — 2014-07-29T15:48:51Z
*** Issue 13202 has been marked as a duplicate of this issue. ***
Comment #5 by davidsp — 2014-07-29T15:50:56Z
I would like to see this disable or if we don't want to break backwards compatibility for this, we should have the compiler issue a warning. As it stands at the moment, the behavior causes more harm then good.
Comment #6 by timon.gehr — 2015-10-07T18:36:08Z
(In reply to Walter Bright from comment #1) > They're actually supposed to be overridable at the moment. I had thought > there might be a use for this, but so far none have materialized. > ... There is one obvious use case: struct S{ @disable enum init=0; } It would be better to have a specific feature here though. E.g. struct S{ @disable init; }
Comment #7 by issues.dlang — 2015-10-07T19:24:59Z
(In reply to timon.gehr from comment #6) > (In reply to Walter Bright from comment #1) > > They're actually supposed to be overridable at the moment. I had thought > > there might be a use for this, but so far none have materialized. > > ... > > There is one obvious use case: > > struct S{ @disable enum init=0; } > > It would be better to have a specific feature here though. E.g. > > struct S{ @disable init; } I would have thought that struct S{ @disable this(); } would do that in addition to making S s; illegal. I was surprised to find out that it didn't. But if there _is_ a good reason for S s = S.init; to still work with @disable this(); was used (though I certainly can't think of one), then @disable init; should probably imply @disable this();.
Comment #8 by dfj1esp02 — 2015-10-08T12:09:04Z
(In reply to timon.gehr from comment #6) > It would be better to have a specific feature here though. E.g. > > struct S{ @disable init; } struct S{ @disable void init(); } ?
Comment #9 by madric — 2018-05-14T06:20:03Z
I encountered this problem as well while porting a C++ library to D. It took a while to diagnose, but I eventually found out that the mere existence of a function named "init" caused the RefAppender I used in a totally unrelated function to break. Example program: ``` import std.array; struct S1 { // The mere presence of this method causes the error, deleting it fixes the error. void init(string p1, int p2, int p3) { } } struct S2 { S1[] a; RefAppender!(int[]) getAppender() { return appender(&a); } } void main() { } ``` The actual error produced is obvious only because the arguments I put on init in this example, but normally it's pretty bizarre: ``` /dlang/dmd/linux/bin64/../../src/phobos/std/array.d(2907): Error: cannot have array of `void(string, int, int)` /dlang/dmd/linux/bin64/../../src/phobos/std/array.d(2976): Error: cannot have array of `inout void(string, int, int)` /dlang/dmd/linux/bin64/../../src/phobos/std/array.d(3369): Error: template instance `std.array.Appender!(S1[])` error instantiating /dlang/dmd/linux/bin64/../../src/phobos/std/array.d(3879): instantiated from here: `RefAppender!(S1[])` onlineapp.d(12): instantiated from here: `appender!(S1[]*, S1)` /dlang/dmd/linux/bin64/../../src/phobos/std/array.d(3429): Error: cannot have array of `inout void(string, int, int)` ```
Comment #10 by nick — 2018-05-14T11:34:32Z
*** Issue 14237 has been marked as a duplicate of this issue. ***
Comment #11 by nick — 2018-05-14T11:41:29Z
struct S { string stringof; } // Issue 14237 class MyClass { void init() {}; } `init` and `stringof` should be required to be `static`, as they are expected to work without an instance. They should probably not be allowed to be `void` functions either.
Comment #12 by nick — 2018-05-14T13:38:46Z
*** Issue 1412 has been marked as a duplicate of this issue. ***
Comment #13 by b2.temp — 2020-01-08T17:10:08Z
*** Issue 17465 has been marked as a duplicate of this issue. ***
Comment #14 by robert.schadek — 2024-12-13T17:57:10Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18386 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB