digitalmars.D.learn - Private nested class instance accessed via outer class public
- Murloc (50/50) Jun 16 2023 I'm not sure if this behavior is desired and have any use-cases
I'm not sure if this behavior is desired and have any use-cases allow this. ```d import std.conv; class Outer { private static class Inner { private int field = 30; override string toString() => to!string(field); } static Inner getInner() { return new Inner(); } } ``` ```d import std.stdio: writeln; import classes; // the file above void main() { // Here I have an object of "some" type (B class cannot be // accessed) and can even do something with it! auto b = Outer.getInner(); writeln(b); // 30 } ``` their class): using System; public class Program { class Outer { private class Inner { private int field = 30; public override string ToString() => field.ToString(); } // Error: Inconsistent accessibility: return type 'Program.Outer.Inner' // is less accessible than method 'Program.Outer.getInner()' public static Inner getInner() { return new Inner(); } } public static void Main() { var b = Outer.getInner(); Console.WriteLine(b); } } ```
Jun 16 2023