Re: import strangeness with std.stdio.write

2018-02-13 Thread Seb via Digitalmars-d-learn

On Tuesday, 13 February 2018 at 16:55:10 UTC, ixid wrote:
On Tuesday, 13 February 2018 at 13:52:37 UTC, rikki cattermole 
wrote:

write exists in both, writeln exists only in std.stdio.

Use named imports to pick which write you want.


It does seem a little silly to have a name clash with such a 
commonly used function. Would it not be better to rename 
std.file.write to something like writeFile and deprecate the 
current name?


Yes, I think it would be worthwhile.
Especially that we will allow importing all of Phobos at once in 
2.079:



https://dlang.org/changelog/pending.html#std-experimental-scripting



Re: import strangeness with std.stdio.write

2018-02-13 Thread ixid via Digitalmars-d-learn
On Tuesday, 13 February 2018 at 13:52:37 UTC, rikki cattermole 
wrote:

write exists in both, writeln exists only in std.stdio.

Use named imports to pick which write you want.


It does seem a little silly to have a name clash with such a 
commonly used function. Would it not be better to rename 
std.file.write to something like writeFile and deprecate the 
current name?


Re: import strangeness with std.stdio.write

2018-02-13 Thread psychoticRabbit via Digitalmars-d-learn

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.



Re: import strangeness with std.stdio.write

2018-02-13 Thread bauss via Digitalmars-d-learn
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");
}


Re: import strangeness with std.stdio.write

2018-02-13 Thread psychoticRabbit via Digitalmars-d-learn

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.



Re: import strangeness with std.stdio.write

2018-02-13 Thread ketmar via Digitalmars-d-learn

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.


Re: import strangeness with std.stdio.write

2018-02-13 Thread psychoticRabbit via Digitalmars-d-learn

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");
}



Re: import strangeness with std.stdio.write

2018-02-13 Thread psychoticRabbit via Digitalmars-d-learn
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)






Re: import strangeness with std.stdio.write

2018-02-13 Thread ketmar via Digitalmars-d-learn

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.


Re: import strangeness with std.stdio.write

2018-02-13 Thread rikki cattermole via Digitalmars-d-learn

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.


Re: import strangeness with std.stdio.write

2018-02-13 Thread psychoticRabbit via Digitalmars-d-learn
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.




import strangeness with std.stdio.write

2018-02-13 Thread psychoticRabbit via Digitalmars-d-learn

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");
}
---