www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - inconsistent behavior with implicit imports

reply "timotheecour" <thelastmammoth gmail.com> writes:
This works:
----
import std.stdio;
void main(){
	writeln(std.conv.to!double(1));
}
----

This doesn't compile:
----
import std.stdio;
void main(){
	std.stdio.writeln(std.conv.to!double(1));
}
----
=>Error: undefined identifier std

So it seems for conv, import std.conv is not needed, but for 
writeln, import std.stdio is needed. Why?

Also, I always get link errors when using those "implicit 
imports" for my own modules and using "rdmd". Is there a way to 
allow this without link errors in the following (using rdmd) ?
----
void main(){
	mypackage.mymodule.myfun(0);
}
----
which would behave as:
----
import mypackage.mymodule;
void main(){
	myfun(0);
}
----
Sep 09 2012
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, September 10, 2012 01:47:35 timotheecour wrote:
 This works:
 ----
 import std.stdio;
 void main(){
 	writeln(std.conv.to!double(1));
 }
 ----
 
 This doesn't compile:
 ----
 import std.stdio;
 void main(){
 	std.stdio.writeln(std.conv.to!double(1));
 }
 ----
 =>Error: undefined identifier std
 
 So it seems for conv, import std.conv is not needed, but for
 writeln, import std.stdio is needed. Why?
 
 Also, I always get link errors when using those "implicit
 imports" for my own modules and using "rdmd". Is there a way to
 allow this without link errors in the following (using rdmd) ?
 ----
 void main(){
 	mypackage.mymodule.myfun(0);
 }
 ----
 which would behave as:
 ----
 import mypackage.mymodule;
 void main(){
 	myfun(0);
 }
 ----

Any and all implicit imports are a _bug_. They should _never_ happen. You should have to import both std.stdio and std.conv. If you don't have to, then you've found a bug. The _only_ exception is when one module publicly imports another, which Phobos _does_ do in a few places, but it definitely doesn't do that here. Nether of your examples should compile. - Jonathan M Davis
Sep 09 2012
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/10/2012 01:47 AM, timotheecour wrote:
 This works:
 ----
 import std.stdio;
 void main(){
      writeln(std.conv.to!double(1));
 }
 ----

 This doesn't compile:
 ----
 import std.stdio;
 void main(){
      std.stdio.writeln(std.conv.to!double(1));
 }
 ----
 =>Error: undefined identifier std

 So it seems for conv, import std.conv is not needed, but for writeln,
 import std.stdio is needed. Why?

I assume because std.stdio imports std.conv, but not vice versa.
 Also, I always get link errors when using those "implicit imports" for
 my own modules and using "rdmd". Is there a way to allow this without
 link errors in the following (using rdmd) ?
 ----
 void main(){
      mypackage.mymodule.myfun(0);
 }
 ----
 which would behave as:
 ----
 import mypackage.mymodule;
 void main(){
      myfun(0);
 }
 ----

This is a quirk of DMD, not a language feature. You can file the implicit import behaviour as a bug.
Sep 09 2012
prev sibling parent "timotheecour" <thelastmammoth gmail.com> writes:
 This is a quirk of DMD, not a language feature.
 You can file the implicit import behaviour as a bug.

done: http://d.puremagic.com/issues/show_bug.cgi?id=8636
Sep 09 2012