Bug?: Presence of "init()" Method Causes std.array.appender to Fail to Compile

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon May 14 01:20:38 UTC 2018


On Monday, May 14, 2018 00:04:20 Uknown via Digitalmars-d wrote:
> On Sunday, 13 May 2018 at 23:53:58 UTC, Vijay Nayar wrote:
> > I encountered a very unexpected error when working on a
> > project.  It seems that the Appender and RefAppender structs
> > created from the std.array.appender() method are sensitive to
> > the mere presence of a method called "init()" on the element
> > type of the array.
> >
> > [...]
>
> init is a reserved function. T.init is the initial value for a
> type. int.init is 0 and float.init is NaN. Try changing your
> function name to initialise or something like that.

Yeah. It's been discussed that it should be illegal to declare a struct or
class member named init, but that change has yet to happen.

https://issues.dlang.org/show_bug.cgi?id=7066
https://issues.dlang.org/show_bug.cgi?id=14237

That mistake was even made in some places in druntime and Phobos (e.g.
TypeInfo in druntime and DirEntry in Phobos), and it caused problems. Those
uses had to be fixed before we could look at actually making it illegal, and
AFAIK, they've all been fixed, but the compiler has yet to be changed. I
don't know if a DIP would be required or not though before it could happen.
It was kind of informally agreed that it should be illegal to declare init,
but I don't think that it was ever officially agreed upon. Regarldless,
declaring init just causes bugs, and code shouldn't do it.

- Jonathan M Davis



More information about the Digitalmars-d mailing list