digitalmars.D - Private Nested Classes?
- D Man (8/8) Mar 31 2019 Is it possible to have private inner nested classes?
- Vasyl Teliman (2/11) Mar 31 2019 Not sure but this looks like a bug.
- Andre Pany (6/21) Mar 31 2019 Private works on module level, not on class level. The inner
- kinke (4/5) Mar 31 2019 It has; `private` in D applies to the module/file. You'll get a
- =?UTF-8?B?UsOpbXkgTW91w6t6YQ==?= (17/26) Mar 31 2019 In D the unit of encapsulation is the module, not the class.
Is it possible to have private inner nested classes? For example if I have a class that is only ever used inside another class, it would make sense to be able to 'lock' it inside the outer class, so that raw instances of it cannot be created. But in this example: https://run.dlang.io/is/PfemUV The `private` specifier seems to have no meaning. Is there a way to get the desired effect (make a nested class only accessable from within it's outer class)?
Mar 31 2019
On Sunday, 31 March 2019 at 20:07:39 UTC, D Man wrote:Is it possible to have private inner nested classes? For example if I have a class that is only ever used inside another class, it would make sense to be able to 'lock' it inside the outer class, so that raw instances of it cannot be created. But in this example: https://run.dlang.io/is/PfemUV The `private` specifier seems to have no meaning. Is there a way to get the desired effect (make a nested class only accessable from within it's outer class)?Not sure but this looks like a bug.
Mar 31 2019
On Sunday, 31 March 2019 at 20:23:55 UTC, Vasyl Teliman wrote:On Sunday, 31 March 2019 at 20:07:39 UTC, D Man wrote:Private works on module level, not on class level. The inner class is not accessible from other modules but from the own module. Kind regards AndreIs it possible to have private inner nested classes? For example if I have a class that is only ever used inside another class, it would make sense to be able to 'lock' it inside the outer class, so that raw instances of it cannot be created. But in this example: https://run.dlang.io/is/PfemUV The `private` specifier seems to have no meaning. Is there a way to get the desired effect (make a nested class only accessable from within it's outer class)?Not sure but this looks like a bug.
Mar 31 2019
On Sunday, 31 March 2019 at 20:07:39 UTC, D Man wrote:The `private` specifier seems to have no meaning.It has; `private` in D applies to the module/file. You'll get a deprecation warning if you try to access it in another file; with `-de`, it becomes an error.
Mar 31 2019
On Sunday, 31 March 2019 at 20:07:39 UTC, D Man wrote:Is it possible to have private inner nested classes? For example if I have a class that is only ever used inside another class, it would make sense to be able to 'lock' it inside the outer class, so that raw instances of it cannot be created. But in this example: https://run.dlang.io/is/PfemUV The `private` specifier seems to have no meaning. Is there a way to get the desired effect (make a nested class only accessable from within it's outer class)?In D the unit of encapsulation is the module, not the class. As the main function resides in the same module as the A.Inner class, it is visible and no compiler error should occur. This rules is meant as a simplification of C++ friend classes and functions. In D, instead of defining friends, one put related class and function into the same module. To "lock" the class, we need separate the main function from class A. This gives a warning (using dmd v2.085.0): main.d(7): Deprecation: a.A.Inner is not visible from module main I notice that Inner is defined as "private static", meaning that you don't intend to use any reference to the enclosing class through `this.outer`. By putting Inner outside of class A, and having main outside of the module, an error occurs: main.d(7): Error: module `main` class a.Inner is private main.d(7): Deprecation: a.Inner is not visible from module main
Mar 31 2019