toString ugliness

Ary Borenszweig ary at esperanto.org.ar
Sun Dec 7 04:12:00 PST 2008


Jarrett Billingsley escribió:
> On Sat, Dec 6, 2008 at 12:11 PM, Jerry <jlquinn at optonline.net> wrote:
>> Jarrett Billingsley Wrote:
>>
>>> On Sat, Dec 6, 2008 at 11:21 AM, Jerry <jlquinn at optonline.net> wrote:
>>>> toString() doesn't work inside a class member function.
>>>>
>>>> import std.string;
>>>> class A {
>>>>  void f() {
>>>>    string s = toString(5);
>>>>  }
>>>> }
>>>>
>>>> This errors with junk.d(19): function object.Object.toString () does not match parameter types (int)
>>>>
>>>> This is a nuisance and a wart (though not a bug per-se).
>>>>
>>>> If the language really can't handle distinguishing this.toString() from toString(int), then std.string.toString really should have a different name.
>> As I'm thinking about this, why does this actually fail?  The parameter doesn't match the prototype for this.toString(), so why not match against the global function?
>>
>> Is this due to the behavior that override solves?  Would marking Object.toString() as override fix the problem?
>>
>>
> 
> As far as I know, it's because D does not use ADL (argument-dependent
> lookup).  That is, it simply looks for the toString that is in the
> nearest enclosing scope, and if it doesn't match - too bad.  Compare
> this behavior to C++, where it would then continue on, looking for a
> definition of toString that does match.  Most of the time ADL leads to
> confusing behavior, which is why it was dropped in D.

But the overloading is obvious! It looks for toString(...) and it founds 
it in the class, but the overloading is wrong. So for me, it should keep 
looking in the enclosing scopes.



More information about the Digitalmars-d mailing list