Hello, I've been having a very strange problem. Ko...
# announcements
s
Hello, I've been having a very strange problem. Kotlin does not find a field in an anonymous object.
Copy code
val man = object : ScriptEngineManager(Thread.currentThread().contextClassLoader) {
	val engine: ScriptEngine inline get() = getEngineByExtension("kts")!!
}
When I try to do
man.engine
, it says
Unresolved reference: engine
, as if it's not there at all. It's very strange. The weirdest thing, is that if I take that code, and copy-paste it into my function, so that
man
is a local variable, it works fine. But if I have it as a field, I cannot access
engine
and I don't know why. There are no naming conflicts with the superclass.
I should also add, I've tried moving the field out of the companion object, and adding
@JvmField
and
@JvmStatic
annotations, to no avail
And if I move it out of the class body entirely, putting it as just a field with no class, that also does not work.
r
man
is a public property, so its type is
ScriptEngineManager
, the declared supertype of the anonymous object
s
Oh. Making it
private
worked. Thanks
r
np
n
alternatively you could do
object Man: ScriptEngineManager(...)
then it will be accessed by its own type and additional properties are there
s
Yeah, I might do that, because now that I've actually run the code I'm getting a horrible error
Copy code
Caused by: java.lang.IllegalStateException: Couldn't build context for private final val man: tech.napkin.sir.superfight.Main.Companion.man.<no name provided> defined in tech.napkin.sir.superfight.Main.Companion[PropertyDescriptorImpl@3eb69742]
	at org.jetbrains.kotlin.codegen.inline.PsiSourceCompilerForInline$Companion.getContext(SourceCompilerForInline.kt:421)
	at org.jetbrains.kotlin.codegen.inline.PsiSourceCompilerForInline$Companion.getContext(SourceCompilerForInline.kt:378)
	at org.jetbrains.kotlin.codegen.inline.PsiSourceCompilerForInline$Companion.getContext(SourceCompilerForInline.kt:378)
	at org.jetbrains.kotlin.codegen.inline.PsiSourceCompilerForInline.initializeInlineFunctionContext(SourceCompilerForInline.kt:351)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.getOrCreateCallGenerator(ExpressionCodegen.java:2911)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.getOrCreateCallGenerator(ExpressionCodegen.java:2972)
	at org.jetbrains.kotlin.codegen.StackValue$Property.putSelector(StackValue.java:1734)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:125)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:118)
	at org.jetbrains.kotlin.codegen.CallReceiver.putSelector(CallReceiver.java:168)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:125)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:118)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.putReceiverAndInlineMarkerIfNeeded(ExpressionCodegen.java:2825)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2734)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2714)
	at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:42)
	at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:41)
	at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:79)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:125)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:118)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.lambda$visitBinaryWithTypeRHSExpression$35(ExpressionCodegen.java:5182)
	at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:79)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:125)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:118)
	at org.jetbrains.kotlin.codegen.CallGenerator$DefaultCallGenerator.genValueAndPut(CallGenerator.kt:71)
	at org.jetbrains.kotlin.codegen.CallBasedArgumentGenerator.generateExpression(CallBasedArgumentGenerator.kt:42)
	at org.jetbrains.kotlin.codegen.ArgumentGenerator.generate(ArgumentGenerator.kt:70)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2743)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2714)
	at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:42)
	at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:41)
	at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:79)
	at org.jetbrains.kotlin.codegen.StackValueWithLeaveTask.putSelector(StackValue.kt:67)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:125)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:118)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.putStackValue(ExpressionCodegen.java:442)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:423)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.lambda$generateTryExpression$34(ExpressionCodegen.java:5033)
	at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:79)
	at org.jetbrains.kotlin.codegen.StackValueWithLeaveTask.putSelector(StackValue.kt:67)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:125)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:118)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.putStackValue(ExpressionCodegen.java:442)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:423)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.lambda$generateIfExpression$0(ExpressionCodegen.java:594)
	at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:79)
	at org.jetbrains.kotlin.codegen.StackValueWithLeaveTask.putSelector(StackValue.kt:67)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:125)
	at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:118)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.putStackValue(ExpressionCodegen.java:442)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:423)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:1846)
	at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$FunctionDefault.doGenerateBody(FunctionGenerationStrategy.java:64)
	at org.jetbrains.kotlin.codegen.coroutines.CoroutineCodegenForLambda$generateResumeImpl$1.doGenerateBody(CoroutineCodegen.kt:569)
	at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$CodegenBased.generateBody(FunctionGenerationStrategy.java:86)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:649)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:484)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:260)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:165)
	at org.jetbrains.kotlin.codegen.coroutines.CoroutineCodegenForLambda.generateResumeImpl(CoroutineCodegen.kt:537)
	at org.jetbrains.kotlin.codegen.coroutines.CoroutineCodegenForLambda.generateClosureBody(CoroutineCodegen.kt:261)
	at org.jetbrains.kotlin.codegen.ClosureCodegen.generateBody(ClosureCodegen.java:188)
	at org.jetbrains.kotlin.codegen.coroutines.CoroutineCodegenForLambda.generateBody(CoroutineCodegen.kt:273)
	at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:129)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genClosure(ExpressionCodegen.java:1104)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genClosure(ExpressionCodegen.java:1069)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitLambdaExpression(ExpressionCodegen.java:1060)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitLambdaExpression(ExpressionCodegen.java:119)
	at org.jetbrains.kotlin.psi.KtLambdaExpression.accept(KtLambdaExpression.java:40)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:316)
	... 85 more