Why public inline functions are called by private constructors

I have a private constructor for my class and implemented invoke on a companion object for a kind of "generic constructor"

class Test private constructor(className: String) {
    companion object {
        // If I remove the internal it fails 
        internal inline operator fun <reified T> invoke(): Test {
            return Test(T::class.java.name) // why can I even call it? The constructor is private
        }
    }
}

and I can even have a public built-in function that calls this common constructor

public inline fun test() = Test<Any>() // Why can I call it, it is internal

doesn't mean that every invokation test()expands to Test(Any::class.java.name), although this constructor is private?

So my questions are:

  • Why internal inline funcan this one call a private constructor? (public pleasure could not)
  • Why does this function public inline funcall an internal function?
  • And why can I end up opening a private constructor in public inline fun?
+4
source share
2
  • ? ( )

internal inline API, , , , , API , (. docs). API inline.

  • ?

. inline API. invoke(). : KT-20223.

  • ?

. , .

+1

. , , , .

, , , . , , , , , - . , , -.

0

Source: https://habr.com/ru/post/1685527/


All Articles