https://kotlinlang.org logo
Title
p

poohbar

01/09/2019, 6:22 PM
I need to call a private constructor of a nested class from a companion defined in the outer class.
h

hudsonb

01/09/2019, 6:46 PM
I don't think that's possible
a

Artem Golovko

01/10/2019, 5:50 AM
If you really want to call private constructor you have to use kotlin-allopen-plugin and use annotations. Or you can use Java for that.
Constructor<Foo> constructor= (Constructor<Foo>) Foo.class.getDeclaredConstructors()[0];
constructor.setAccessible(true); 
obj = constructor.newInstance("foo");
But in you case if you want define an internal class in the parent class, I think, you should use
private class Child()
and you can create Child class everywhere in your Parent class, but you can't create Child outside the Parent class, because it's private. But according for your code whatever you want it's hide constructor of the class. And you can use something like this.
class Child private constructor () {
companion object {
        operator fun invoke(): Child {
            return Child()
        }
    }
}
class Parent() {
companion object {
        operator fun foo = Child()
    }
}