Pathological import symbol shadowing
Paul Backus
snarwin at gmail.com
Wed Nov 18 01:19:16 UTC 2020
On Wednesday, 18 November 2020 at 01:11:35 UTC, Walter Bright
wrote:
>> It's not just rejects-valid either, this issue has
>> accepts-invalid cases:
>>
>> ---
>> import std.stdio;
>>
>> string readAndLog(string filename){
>> import std.file;
>> auto text=readText(filename);
>> write(filename," read successfully!\n");
>> return text;
>> }
>>
>> void main(){
>> writeln(readAndLog("important_data.txt"));
>> }
>> ---
>
> Please elaborate on what is invalid about it? Don't make me
> guess!
Because std.file is imported locally, the call to write calls
std.file.write instead of std.stdio.write, and overwrites
important_data.txt.
If `import std.file` is moved to module scope, the compiler
correctly diagnoses that the call is ambiguous.
More information about the Digitalmars-d
mailing list