How can I make nested class constructors accessible only by surrounding class?

Now that I see this post I remember I had the same problem a while ago. I didn’t find a good solution. I ended up with an internal constructor, maybe this is a workable solution for you as well.

I’m not sure I like the idea of private[this], private[Something] though. It feels a bit too complex for something minor.
I personally would be fine with allowing maybe the inner keyword to be used as a visibility modifier as long as the class is nested. (inner private fun foo() = TODO())

On the JVM this could probably be achieved the same way as internal. A public function which is marked as synthetic, so that java won’t call it, plus a rule in kotlin that you can only call it from within the nested or outer class.

1 Like