Expression is inaccessible from a nested class

Hello.

Could you please explain the difference between code fragments:

Compiles and works well:

class Test(val value: Int) {
  class object {
  fun create(init: () -> Int): Test {
           return Test(init.invoke())
  }
  }
}


Failes with “Expression is inaccessible from a nested class ‘<class-object-for-Test>’, use ‘inner’ keyword to make the class inner” on init()

class Test(val value: Int) {
  class object {
  fun create(init: () -> Int): Test {
           return Test(init())
  }
  }
}


Thanks,
Alexey

The explanation is rather simple: it's a bug :) Please, report to the tracker. Thanks

Done: KT-3647

1 Like

import kotlin.reflect.KClass
import kotlin.reflect.full.companionObject

interface Comp {
val type: Int
}

open class A {
companion object: Comp{
override val type = 1
}
}

fun <T: A> getType(kClass: KClass): Int {
return (kClass.companionObject as? Comp)?.type ?: 0
}

open class B : A() {
companion object: Comp{
override val type: Int = 2
}
}

open class Generic<T: A>(private val kClass: KClass){
fun hello(){
getType(kClass) // Expression is inaccessible from a nested class ‘Generic’
}
}

val g = Generic(B::class)
g.hello()