D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7066 - You can redefine .init and .stringof without error
Summary: You can redefine .init and .stringof without error
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: spec
: 1412 8817 13202 14237 17465 (view as issue list)
Depends on:
Blocks: 12545 12233
  Show dependency treegraph
 
Reported: 2011-12-04 15:44 UTC by Peter Alexander
Modified: 2024-12-13 17:57 UTC (History)
11 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Peter Alexander 2011-12-04 15:44:59 UTC
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 Walter Bright 2012-01-21 01:11:11 UTC
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 Jesse Phillips 2014-02-24 22:00:11 UTC
How about it is disable until a compelling argument is provided. Or if someone complains during a beta.
Comment 3 Andrej Mitrovic 2014-04-21 19:45:18 UTC
*** Issue 8817 has been marked as a duplicate of this issue. ***
Comment 4 davidsp 2014-07-29 15:48:51 UTC
*** Issue 13202 has been marked as a duplicate of this issue. ***
Comment 5 davidsp 2014-07-29 15:50:56 UTC
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 timon.gehr 2015-10-07 18:36:08 UTC
(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 Jonathan M Davis 2015-10-07 19:24:59 UTC
(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 anonymous4 2015-10-08 12:09:04 UTC
(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 Vijay Nayar 2018-05-14 06:20:03 UTC
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 Nick Treleaven 2018-05-14 11:34:32 UTC
*** Issue 14237 has been marked as a duplicate of this issue. ***
Comment 11 Nick Treleaven 2018-05-14 11:41:29 UTC
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 Nick Treleaven 2018-05-14 13:38:46 UTC
*** Issue 1412 has been marked as a duplicate of this issue. ***
Comment 13 basile-z 2020-01-08 17:10:08 UTC
*** Issue 17465 has been marked as a duplicate of this issue. ***
Comment 14 dlangBugzillaToGithub 2024-12-13 17:57:10 UTC
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