Bug 1412 – stringof shouldn't be shadowed by member func

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-08-11T09:35:55Z
Last change time
2018-05-14T13:38:46Z
Keywords
accepts-invalid
Assigned to
Walter Bright
Creator
david

Comments

Comment #0 by davidl — 2007-08-11T09:35:55Z
import std.stdio; struct k { char[] stringof() { return "mystringof"; } } void main() { k v; writefln(v.stringof); }
Comment #1 by bugzilla — 2007-08-11T12:36:29Z
The builtin properties can be overridden by the user if he desires. This is working as designed.
Comment #2 by davidl — 2007-08-11T18:38:24Z
this design would break generic programing. Consider if users are not aware of this design feature, they use stringof as their private internal use? And I still don't know why a user want to overload stringof? For me, overloading these builtin properties could only be inconsistent
Comment #3 by bugzilla — 2007-08-11T19:00:46Z
It's designed this way so the class designer can customize what the properties do.
Comment #4 by davidl — 2008-05-11T20:59:24Z
i will appreciate that a warning or a explicit override const keyword? struct k { override const char[] stringof() // for overloading the stringof builtin prop,users have to use "override const" keyword explicitly? { return "mystringof"; } } or the compiler emits a warning will be nice also
Comment #5 by bruno.do.medeiros+deebugz — 2008-05-27T17:29:21Z
But unlike "init" or others, stringof is not a real property, nor anything like it. It is a special, meta-programming construct that has a syntax like a property, but behaves quite differently from a property, due to the fact that the stringof expression is never evaluated. For example such expression: (new Foo()).stringof does not create a new Foo. That alone shows the difference in semantics. It's not even possible to define a custom stringof property that behaves like the original stringof! So the language should not allow "redefining" the stringof property. It should be the same as sizeof, which cannot be redefined (and also does not evaluate it's expression). (One could also suggest that a free function syntax, ie: "sizeof(new A())", should be allowed, and maybe even prefered)
Comment #6 by nick — 2018-05-14T13:38:46Z
*** This issue has been marked as a duplicate of issue 7066 ***