Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 13, 2018 import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
So, strange problem below. The commented-out line will not compile (if I un-comment it), unless I either move std.stdio into main, or, move std.file out of main. Whereas writeln works just fine as is. --------------------- module test; import std.stdio; void main() { import std.file; //write("hello"); writeln("hello again"); } ----------------------- |
February 13, 2018 Re: import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
Posted in reply to psychoticRabbit | On 13/02/2018 1:46 PM, psychoticRabbit wrote:
> So, strange problem below.
>
> The commented-out line will not compile (if I un-comment it), unless I either move std.stdio into main, or, move std.file out of main.
>
> Whereas writeln works just fine as is.
>
> ---------------------
> module test;
>
> import std.stdio;
>
> void main()
> {
> import std.file;
>
> //write("hello");
> writeln("hello again");
> }
> -----------------------
write exists in both, writeln exists only in std.stdio.
Use named imports to pick which write you want.
|
February 13, 2018 Re: import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
Posted in reply to psychoticRabbit | On Tuesday, 13 February 2018 at 13:46:11 UTC, psychoticRabbit wrote:
> So, strange problem below.
>
> The commented-out line will not compile (if I un-comment it), unless I either move std.stdio into main, or, move std.file out of main.
>
> Whereas writeln works just fine as is.
>
> ---------------------
> module test;
>
> import std.stdio;
>
> void main()
> {
> import std.file;
>
> //write("hello");
> writeln("hello again");
> }
> -----------------------
I should add, that this problem seems directly related to std.file.
i.e. if I replace std.file, with say, std.conv, then the write function (once uncommented) will compile ok.
|
February 13, 2018 Re: import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On Tuesday, 13 February 2018 at 13:52:37 UTC, rikki cattermole wrote:
> On 13/02/2018 1:46 PM, psychoticRabbit wrote:
>> So, strange problem below.
>>
>> The commented-out line will not compile (if I un-comment it), unless I either move std.stdio into main, or, move std.file out of main.
>>
>> Whereas writeln works just fine as is.
>>
>> ---------------------
>> module test;
>>
>> import std.stdio;
>>
>> void main()
>> {
>> import std.file;
>>
>> //write("hello");
>> writeln("hello again");
>> }
>> -----------------------
>
> write exists in both, writeln exists only in std.stdio.
>
> Use named imports to pick which write you want.
oh..you must have posted as I why posting ;-)
That makes sense then. Thanks for clearing that up.
And I should have read the compiler message more clearly..cause the answer was in that error message (more or less)
|
February 13, 2018 Re: import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
Posted in reply to psychoticRabbit | psychoticRabbit wrote:
> So, strange problem below.
>
> The commented-out line will not compile (if I un-comment it), unless I either move std.stdio into main, or, move std.file out of main.
>
> Whereas writeln works just fine as is.
>
> ---------------------
> module test;
>
> import std.stdio;
>
> void main()
> {
> import std.file;
>
> //write("hello");
> writeln("hello again");
> }
> -----------------------
`std.file` has function named `write()` too. and local import completely shadows global imports (i.e. it removes global imports from overload set for the given scope), hence `std.stdio.write()` is not available there.
this is done by purpose, so your code won't accidentally use wrong function. you can bring `std.stdio` functions back by adding local `import std.stdio;`, for example.
|
February 13, 2018 Re: import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Tuesday, 13 February 2018 at 13:57:38 UTC, ketmar wrote:
> `std.file` has function named `write()` too. and local import completely shadows global imports (i.e. it removes global imports from overload set for the given scope), hence `std.stdio.write()` is not available there.
"..local import completely shadows global imports"
oh... I didn't realised imports are subject to scope rules in that way.
This new knowledge will certainly prevent some ongoing confusion ;-)
thanks.
Also, if I do this below, how does the compiler choose the correct write function?
import std.stdio;
import std.file;
void main()
{
write("hello");
writeln("hello again");
}
|
February 13, 2018 Re: import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
Posted in reply to psychoticRabbit | psychoticRabbit wrote:
> Also, if I do this below, how does the compiler choose the correct write function?
>
> import std.stdio;
> import std.file;
>
> void main()
> {
> write("hello");
> writeln("hello again");
> }
it's easy: just take a look at `std.file.write()`. first, it require two arguments. this is enough to rule `stf.file.write()` out in your case.
|
February 13, 2018 Re: import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
Posted in reply to psychoticRabbit | On Tuesday, 13 February 2018 at 13:56:17 UTC, psychoticRabbit wrote:
> On Tuesday, 13 February 2018 at 13:52:37 UTC, rikki cattermole wrote:
>> On 13/02/2018 1:46 PM, psychoticRabbit wrote:
>>> So, strange problem below.
>>>
>>> The commented-out line will not compile (if I un-comment it), unless I either move std.stdio into main, or, move std.file out of main.
>>>
>>> Whereas writeln works just fine as is.
>>>
>>> ---------------------
>>> module test;
>>>
>>> import std.stdio;
>>>
>>> void main()
>>> {
>>> import std.file;
>>>
>>> //write("hello");
>>> writeln("hello again");
>>> }
>>> -----------------------
>>
>> write exists in both, writeln exists only in std.stdio.
>>
>> Use named imports to pick which write you want.
>
> oh..you must have posted as I why posting ;-)
>
> That makes sense then. Thanks for clearing that up.
>
> And I should have read the compiler message more clearly..cause the answer was in that error message (more or less)
What you can do is use aliases to use both functions.
import io = std.stdio;
void main()
{
import file = std.file;
file.write("hello");
io.writeln("hello again");
}
|
February 13, 2018 Re: import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Tuesday, 13 February 2018 at 14:18:05 UTC, ketmar wrote:
> psychoticRabbit wrote:
>
>> Also, if I do this below, how does the compiler choose the correct write function?
>>
>> import std.stdio;
>> import std.file;
>>
>> void main()
>> {
>> write("hello");
>> writeln("hello again");
>> }
>
> it's easy: just take a look at `std.file.write()`. first, it require two arguments. this is enough to rule `stf.file.write()` out in your case.
oh..function overloading..makes sense.
thanks again.
|
February 13, 2018 Re: import strangeness with std.stdio.write | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | On Tuesday, 13 February 2018 at 14:21:31 UTC, bauss wrote:
>
> What you can do is use aliases to use both functions.
>
> import io = std.stdio;
>
> void main()
> {
> import file = std.file;
>
> file.write("hello");
> io.writeln("hello again");
> }
that's a nice simple solution.
thanks.
|
Copyright © 1999-2021 by the D Language Foundation