Name lookups in D
Steven Schveighoffer
schveiguy at gmail.com
Mon Dec 14 18:54:58 UTC 2020
On 12/14/20 12:59 PM, kdevel wrote:
> On Monday, 14 December 2020 at 16:21:56 UTC, Steven Schveighoffer wrote:
>> This is expected behavior.
>
> Sure. In the original code the missing downcast was not that easily
> discernible.
I understand. There is sometimes confusion when a ufcs "member" is
found. I've seen a lot of weird things like this.
But the compiler can only tell what you want from the code you write, it
can't look at the intentions behind it. Especially when you make a mistake.
I've had countless times where I reach for something.to!somethingelse
and I forgot to import std.conv, but I've happened to import
std.datetime (which has a similar ufcs `to` function). And the error
message is always confusing.
I don't think this is fixable. You just have to live with it. It's
unfortunate that it compiles, and doesn't give an error. "Best effort"
functions like text that *always* compile can be hard to diagnose when
you call them by accident.
>> B does not have a member named "text", and so it looks for a UFCS
>> function to call and finds one.
>>
>> Note that a static import means you have to use the FQN to call text
>> (but text is perfectly fine via UFCS). However, a selective import
>> would still call text as expected.
>
> The original code accessed only std.conv.to. A selective import
> of `to` would have complained and helped to spot the error:
OK, I thought you meant a selective import of std.conv.text.
>
> q.d(13): Error: no property text for type q.B, perhaps import std.conv;
> is needed?
>
> BTW: Most of the time dmd's guess is accurate.
Those suggestions are hard-coded into the compiler, and I sometimes wish
they were more comprehensive. But I don't see any future in which the
compiler is going to suggest "maybe you meant to downcast to type D". It
just can't see what you were thinking.
-Steve
More information about the Digitalmars-d
mailing list