www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A question about the relation between different module.

reply Yonggang Luo <yonggangluo hotmail.com> writes:
I have a file that named 
baseClass.d
The content is
module baseClass;

public class Base
{
}

public void Function()
{
}


private class BaseAlternative
{
}

private void FunctionAlternative()
{
}

And the other file that named
subClass.d
The content is
module subClass;
private import baseClass;

class Sub:Base /*Is this legal?*/
{
};


void DoSomeThins()
{
	Function(); /*Is this legal?*/
}
Nov 11 2008
next sibling parent reply Yonggang Luo <yonggangluo hotmail.com> writes:
I have a file that named 
baseClass.d
The content is
module baseClass;

public class Base
{
}

public void Function()
{
}


private class BaseAlternative
{
}

private void FunctionAlternative()
{
}

And the other file that named
subClass.d
The content is
module subClass;
private import baseClass; /*!!!!!!!! Please notice, this is private import*/

class Sub:Base /*Is this legal?*/
{
};


void DoSomeThins()
{
	Function(); /*Is this legal?*/
}


private void DoPrivateFunctions(Base x) /*Is this legal?*/
{
}

public void DoPublicFunctions(Base x) /*Is this legal?*/
{
}

private alias Base BaseAlternative; /*Is this legal?*/

public alias Base PublicBase; /*Is this legal?*/
Nov 11 2008
next sibling parent ore-sama <spam here.lot> writes:
you can ask compiler too.
Nov 11 2008
prev sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Tue, Nov 11, 2008 at 10:56 AM, Yonggang Luo <yonggangluo hotmail.com> wrote:
 And the other file that named
 subClass.d
 The content is
 module subClass;
 private import baseClass; /*!!!!!!!! Please notice, this is private import*/
"private import" only means that if another module, say "main," imports subClass, it won't see baseClass unless it does "import baseClass;". It does not change what you can access out of baseClass.
 class Sub:Base /*Is this legal?*/
 {
 };
Once again, yes.
 void DoSomeThins()
 {
        Function(); /*Is this legal?*/
 }
Yes, again.
 private void DoPrivateFunctions(Base x) /*Is this legal?*/
 {
 }
Of course.
 public void DoPublicFunctions(Base x) /*Is this legal?*/
 {
 }
Yes.
 private alias Base BaseAlternative; /*Is this legal?*/
Yes!
 public alias Base PublicBase; /*Is this legal?*/
Yes! As ore-sama said, please try compiling these things before asking. Also, please put learning questions on digitalmars.D.learn, not on digitalmars.D.
Nov 11 2008
parent reply Yonggang Luo <yonggangluo hotmail.com> writes:
Jarrett Billingsley Wrote:

 On Tue, Nov 11, 2008 at 10:56 AM, Yonggang Luo <yonggangluo hotmail.com> wrote:
 And the other file that named
 subClass.d
 The content is
 module subClass;
 private import baseClass; /*!!!!!!!! Please notice, this is private import*/
"private import" only means that if another module, say "main," imports subClass, it won't see baseClass unless it does "import baseClass;". It does not change what you can access out of baseClass.
 class Sub:Base /*Is this legal?*/
 {
 };
Once again, yes.
This is not just a question. I think this must be illegal.. Why? Because we are "private import baseClass;" and Now we create the third file that named thirdClass.d; The content is. module thirdClass; private import subClass; so if class Sub:Base is legal, then we also can access Base. But the fact is "private import Base"; from this clue , we can't access Base Class. So it's confusing.
 
 void DoSomeThins()
 {
        Function(); /*Is this legal?*/
 }
Yes, again.
 private void DoPrivateFunctions(Base x) /*Is this legal?*/
 {
 }
Of course.
 public void DoPublicFunctions(Base x) /*Is this legal?*/
 {
 }
Yes.
/*Also is here */
 
 private alias Base BaseAlternative; /*Is this legal?*/
Yes!
 public alias Base PublicBase; /*Is this legal?*/
Yes!
/*/*Also is here */
 
 As ore-sama said, please try compiling these things before asking.
 
 Also, please put learning questions on digitalmars.D.learn, not on
 digitalmars.D.
Nov 11 2008
next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
module baseClass;

public class base {}

// end file

module subClass;

private import baseClass

public class sub : base {} // legal. base is imported from baseClass.

// end file

module subsubClass;

private import subClass;

public class subsub : sub {} // legal. sub is imported from subClass.

public class subsub2 : base {} // illegal. base is not imported, as it is  
a private import in subClass.

// end file

All public members of a module are imported with "private import X;" in  
module Y, but none are visible to modules that are in turn importing  
module Y.

-- 
Simen
Nov 11 2008
prev sibling next sibling parent Kagamin <spam here.lot> writes:
Yonggang Luo Wrote:

 This is not just a question. 
 I think this must be illegal..
 Why? 
 Because we are "private import baseClass;"
private import affects only import, not imported classes, if you want Base to be private, declare *it* private. You can't affect type declarations just by importing them.
 and Now we create the third file that named thirdClass.d;
 The content is.
 
 module thirdClass;
 private import subClass;
 
 so if  class Sub:Base is legal, then we also can access Base.
And we can, in fact.
 But the  fact is "private import Base";
 from this clue , we can't access Base Class.
 So it's confusing.
If you can't access anything through private import, lol, why you would need it?
Nov 12 2008
prev sibling next sibling parent "Nick Sabalausky" <a a.a> writes:
"Yonggang Luo" <yonggangluo hotmail.com> wrote in message 
news:gfdkcs$1all$1 digitalmars.com...
 But the  fact is "private import Base";
 from this clue , we can't access Base Class.
 So it's confusing.
"private import" works the same as "private class" and "private foo()". It means "This module can access it, but nothing else can." ---------------------------------------- module ModuleA; void fooA() { } //end of file ---------------------------------------- module ModuleB; void fooB() { } //end of file ---------------------------------------- module ModuleLibrary; // ModuleLibrary can access all of these // Anything that imports ModuleLibrary can access all of these import ModuleA; class ClassA {} FunctionA() {} int VariableA; // ModuleLibrary can access all of these // Anything that imports ModuleLibrary can NOT access any of these private import ModuleB; private class ClassB {} private FunctionB() {} private int VariableB; //end of file ---------------------------------------- module ModuleMain; import ModuleLibrary; //All ok: fooA(); auto cA = new ClassA(); FunctionA(); VariableA = 1; //All error: fooB(); auto cB = new ClassB(); FunctionB(); VariableB = 1; //end of file ---------------------------------------- So you see, "private import" works just like "private class", "private Function()" and "private int".
Nov 12 2008
prev sibling parent reply Yonggang Luo <yonggangluo hotmail.com> writes:
//--------------
//start file A.d
module A;
public class Base{}
public void baseFunction{}

//end file A.d
//--------------


//-------------
//start file B.d
module B;
private import A;
public void FunctionB(Base x); //at least this is illegal. Because ...
public Base FunctionElse(); /*This is also be illegal, the same reason*/

public class B:Base{}; /*I think this is illegal should be better*/
/* This means we can't access a private module through any way.*/
//end file B.d
//-------------


//-------------
//start file C.d  this fele is the reason...
module C;
private import B;
private void FunctionC()
{
	Base x; //illegal
	//so you can't access FunctionB(x); /*Because x can't be define*/
}
//end file C.d
Nov 14 2008
parent mgen <bmeck stedwards.edu> writes:
I'm sorry but a private import is not a private module. A private import is
seen as private by external modules and as such cannot be accessed. Also the
import would be meaningless if it changed the access privileges of any module
since by your logic public import would change private functions to public. If
you are trying to make a module that cannot have anything imported from it make
everything inside of the module private.
Nov 14 2008
prev sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Tue, Nov 11, 2008 at 10:53 AM, Yonggang Luo <yonggangluo hotmail.com> wrote:
 I have a file that named
 baseClass.d
 The content is
 module baseClass;

 public class Base
 {
 }

 public void Function()
 {
 }


 private class BaseAlternative
 {
 }

 private void FunctionAlternative()
 {
 }

 And the other file that named
 subClass.d
 The content is
 module subClass;
 private import baseClass;

 class Sub:Base /*Is this legal?*/
 {
 };
Of course it's legal. "Base" is public.
 void DoSomeThins()
 {
        Function(); /*Is this legal?*/
 }
Again, yes, this is legal, since "Function" is public. Anything that is public is accessible from other modules.
Nov 11 2008