www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - import strangeness with std.stdio.write

reply psychoticRabbit <meagain meagain.com> writes:
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");
}
-----------------------
Feb 13 2018
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
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.
Feb 13 2018
next sibling parent reply psychoticRabbit <meagain meagain.com> writes:
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)
Feb 13 2018
parent reply bauss <jj_1337 live.dk> writes:
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"); }
Feb 13 2018
parent psychoticRabbit <meagain meagain.com> writes:
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.
Feb 13 2018
prev sibling parent reply ixid <nuaccount gmail.com> writes:
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?
Feb 13 2018
parent Seb <seb wilzba.ch> writes:
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
Feb 13 2018
prev sibling next sibling parent psychoticRabbit <meagain meagain.com> writes:
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.
Feb 13 2018
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
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.
Feb 13 2018
parent reply psychoticRabbit <meagain meagain.com> writes:
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"); }
Feb 13 2018
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
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.
Feb 13 2018
parent psychoticRabbit <meagain meagain.com> writes:
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.
Feb 13 2018