Name lookups in D
kdevel
kdevel at vogtner.de
Mon Dec 14 17:59:46 UTC 2020
On Monday, 14 December 2020 at 16:21:56 UTC, Steven Schveighoffer
wrote:
[...]
>> Just 'implemented' the following bug:
>>
>> ~~~
>> import std.conv;
>>
>> class B {
>> }
>>
>> class D : B {
>> string text;
>> }
>>
>> void main ()
>> {
>> B b = new D;
>> string s = b.text;
>> }
>> ~~~
>>
>> Due to UFCS std.conv.text grabs the object. Only a `static
>> import`
>> or a selective import would reveal the problem during compile
>> time.
>
> This is expected behavior.
Sure. In the original code the missing downcast was not that
easily
discernible.
> 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:
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.
More information about the Digitalmars-d
mailing list