Jump to page: 1 2
Thread overview
import strangeness with std.stdio.write
Feb 13
bauss
Feb 13
ixid
Feb 13
Seb
Feb 13
ketmar
Feb 13
ketmar
February 13
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
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
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
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
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
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
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
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
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
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.

« First   ‹ Prev
1 2