| Thread overview | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 11, 2008 A question about the relation between different module. | ||||
|---|---|---|---|---|
| ||||
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?*/
}
| ||||
November 11, 2008 A updated version. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yonggang Luo | 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?*/
| |||
November 11, 2008 Re: A question about the relation between different module. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yonggang Luo | 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. | |||
November 11, 2008 Re: A updated version. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yonggang Luo | you can ask compiler too. | |||
November 11, 2008 Re: A updated version. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yonggang Luo | 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. | |||
November 12, 2008 This is not just a question. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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. | |||
November 12, 2008 Re: This is not just a question. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yonggang Luo | 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
| |||
November 12, 2008 Re: This is not just a question. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yonggang Luo | 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? | |||
November 12, 2008 Re: This is not just a question. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yonggang Luo | "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". | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply