www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - private import in classes 2 examples

reply Antonio Monteiro <duitoolkit yahoo.ca> writes:
example 1
compiles and run. shoult it be legal? I don't think so.
(all one module)
###########################
class A
{
	private import std.stdio;
	this()
	{
		writefln("A.this");
	}
}

class B : A
{
	this()
	{
		writefln("B.this");
	}
}

void main()
{
	new A;
	new B;
}
##############################
##############################
example 2
command:  dmd impa.d impb.d -I~/dmd/src/phobos
fails to compile - expected same result as example 1
2 modules
##### module impa.d
class A
{
	private import std.stdio;
	this()
	{
		writefln("A.this");
	}
}
######### modules impb.d
import impa;

class B
{
	this()
	{
		writefln("A.this");
	}
}

void main()
{
	new A;
	new B;
}
##############

Ant
Apr 06 2005
next sibling parent reply Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Antonio Monteiro wrote:

 example 1
 compiles and run. shoult it be legal? I don't think so.
 (all one module)
 ###########################
 class A
 {
 private import std.stdio;
 this()
 {
 writefln("A.this");
 }
 }
 
 class B : A
 {
 this()
 {
 writefln("B.this");
 }
 }
 
 void main()
 {
 new A;
 new B;
 }
 ##############################

ImportDeclaration are only allowed on the module scope. Heck, strictly speaking the code below is illegal # version(Foo){ # import some.modules; # } It seems to be the intention to allow imports at all levels: http://digitalmars.com/d/module.html # The top level scope in the (imported) module is merged with the current # scope. Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCU55Q3w+/yD4P9tIRAhvRAJsHue7pYvV1PSP6Fk5LHj0Lzl2t9wCfRCC+ XWiJriG662WZu+D9EpgRBzQ= =u13w -----END PGP SIGNATURE-----
Apr 06 2005
parent Ant <Ant_member pathlink.com> writes:
In article <rbjci2-er5.ln1 lnews.kuehne.cn>, Thomas Kuehne says...
It seems to be the intention to allow imports at all levels:
http://digitalmars.com/d/module.html
# The top level scope in the (imported) module is merged with the current
# scope.

From your other post I thought it was allowed only at the module level... I have to read the docs again. So, your new import nocompile tests need to be reviewd? Ant
Apr 06 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 06 Apr 2005 03:58:18 -0400, Antonio Monteiro wrote:

 example 1
 compiles and run. shoult it be legal? I don't think so.
 (all one module)
 ###########################
 class A
 {
 	private import std.stdio;
 	this()
 	{
 		writefln("A.this");
 	}
 }
 
 class B : A
 {
 	this()
 	{
 		writefln("B.this");
 	}
 }
 
 void main()
 {
 	new A;
 	new B;
 }
 ##############################
 ##############################
 example 2
 command:  dmd impa.d impb.d -I~/dmd/src/phobos
 fails to compile - expected same result as example 1
 2 modules
 ##### module impa.d
 class A
 {
 	private import std.stdio;
 	this()
 	{
 		writefln("A.this");
 	}
 }
 ######### modules impb.d
 import impa;
 
 class B
 {
 	this()
 	{
 		writefln("A.this");
 	}
 }
 
 void main()
 {
 	new A;
 	new B;
 }
 ##############
 
 Ant

I can't reproduce it here. (Windows XP, DMD 0.119) This is what I get ... ________________________________________________ C:\temp>type impa.d class A { private import std.stdio; this() { writefln("A.this"); } } C:\temp>type impb.d import impa; class B : A { this() { writefln("B.this"); } } void main() { new A; new B; } C:\temp>dmd impa.d impb.d c:\dparnell\dmd\bin\..\..\dm\bin\link.exe impa+impb,,,user32+kernel32/noi; C:\temp>impb A.this A.this B.this C:\temp> _____________________________________ I didn't expect it to fail, as the class B is extending the class A, and class A can actually see the stdio import. However, when I moved it out of the class, but still kept it private, the compile failed, as I expected it would. -- Derek Melbourne, Australia 6/04/2005 6:08:58 PM
Apr 06 2005
parent Ant <Ant_member pathlink.com> writes:
In article <1dqfstzg9wg1b$.xuf932uvz27u$.dlg 40tude.net>, Derek Parnell says...

I can't reproduce it here. (Windows XP, DMD 0.119) This is what I get ...
________________________________________________
C:\temp>type impa.d
class A
{
        private import std.stdio;
        this()
        {
                writefln("A.this");
        }
}

C:\temp>type impb.d
import impa;
class B : A
{
        this()
        {
                writefln("B.this");
        }
}
void main()
{
        new A;
        new B;
}

C:\temp>dmd impa.d impb.d
c:\dparnell\dmd\bin\..\..\dm\bin\link.exe impa+impb,,,user32+kernel32/noi;

C:\temp>impb
A.this
A.this
B.this

C:\temp>
_____________________________________

I didn't expect it to fail, as the class B is extending the class A, and
class A can actually see the stdio import. However, when I moved it out of
the class, but still kept it private, the compile failed, as I expected it
would.

Sorry, I'm at linux with dmd 0.119 (I didn't expect this to be differente on window and linux, that's exactly why we should always report our environmnet...) So I would say that's another bug as the import declaration should be marked private and not visible on class B. Is protected respect on import? It would only make sence for imports on the class body. That would be nice, wouldn't it? or not? Ant
Apr 06 2005