kotlin - Why can public inline functions call private constructors -
i have private constructor class , implemented invoke on companion object kind of "generic constructor"
class test private constructor(classname: string) { companion object { // if remove internal fails internal inline operator fun <reified t> invoke(): test { return test(t::class.java.name) // why can call it? constructor private } } }
and can have public inline function calls generic constructor
public inline fun test() = test<any>() // why can call it, internal
shouldn't mean every invokation test()
expands test(any::class.java.name)
though constructor private?
so questions are:
- why can
internal inline fun
call private constructor? (a public fun couldn't) - why can
public inline fun
call internal function? - and why can expose private constructor in
public inline fun
?
- why can internal inline fun call private constructor? (a public fun couldn't)
an internal
inline
function can access non-public api, because can called in same module, , won't suffer binary incompatibility might introduced when api changes , calling module not re-compiled (see docs). such calls prohibited public api inline
functions.
- why can public inline fun call internal function?
it's bug. normally, public inline
function cannot access non-public api. rule seems not checked invoke()
operator calls. i've submitted issue: kt-20223.
- and why can expose private constructor in public inline fun?
that's due composition of expected behavior in first point , bug in second point. ultimately, it's bug.
Comments
Post a Comment