Bug 14237 – Compiler should reject attempts to (re)define .init

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-03-02T16:55:43Z
Last change time
2018-05-14T11:34:32Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
heartcollector.87
See also
https://issues.dlang.org/show_bug.cgi?id=7066

Comments

Comment #0 by heartcollector.87 — 2015-03-02T16:55:43Z
This sample program will create a really difficult-to-understand error message. import std.container; class MyClass { void init() {}; } private Array!MyClass temp; void main() {} /opt/compilers/dmd2/include/std/container.d(2562): Error: template std.algorithm.initializeAll cannot deduce function from argument types !()(MyClass[]), candidates are: /opt/compilers/dmd2/include/std/algorithm.d(1266): std.algorithm.initializeAll(Range)(Range range) if (isInputRange!Range && hasLvalueElements!Range && hasAssignableElements!Range) /opt/compilers/dmd2/include/std/algorithm.d(1294): std.algorithm.initializeAll(Range)(Range range) if (is(Range == char[]) || is(Range == wchar[])) /opt/compilers/dmd2/include/std/container.d(3391): Error: template std.algorithm.copy cannot deduce function from argument types !()(Range, Range), candidates are: /opt/compilers/dmd2/include/std/algorithm.d(7497): std.algorithm.copy(Range1, Range2)(Range1 source, Range2 target) if (isInputRange!Range1 && isOutputRange!(Range2, ElementType!Range1)) DMD does not complain if a user declares a init() member function for a class, even if init() is a property defined for every Type. This can cause really ugly errors as the one displayed above. Usually trying to define a property and a function with the same name results in a much nicer error.
Comment #1 by hsteoh — 2015-03-02T17:55:08Z
The compiler should reject attempts to define a member named 'init', because it conflicts with the special meaning of .init in the language.
Comment #2 by hsteoh — 2015-03-02T17:59:35Z
Comment #3 by nick — 2018-05-14T11:34:32Z
*** This issue has been marked as a duplicate of issue 7066 ***