I have the code below. It is Kotlin. Any idea why textToSpeech
from textToSpeech.setLanguage(Locale.UK)
is telling that there is no reference resolved for textToSpeech
?
val textToSpeech = TextToSpeech(
applicationContext,
object : TextToSpeech.OnInitListener {
override fun onInit(status: Int) {
if (status == TextToSpeech.SUCCESS) {
textToSpeech.setLanguage(Locale.UK)
}
}
})
At first I assumed it is an Idea kotlin plugin bug, but it seems that it actually can't be compiled
Kotlin has hardened the variables initialization policy, and it's now prohibited to reference the variable inside its initializer, even in lambdas and object expressions, which seems reasonable: imagine that a lambda is called immediately before the variable assignment.
For your case, I can suggest as a workaround using an object expression in this quite cumbersome construct:
This will initialize an anonymous object with
inner
inside that is acceptable throughvalue
property. Note that theinner
initializer usesvalue
property. Then thevalue
is extracted and can be used.But please keep in mind that this trick is unsafe: in runtime, using
value
beforeinner
is assigned (e.g. inTextToSpeech
constructor) will throwNullPointerException
.Also, I've replaced the
OnInitListener
with a lambda using SAM conversion to be short, but object expression can still be used there.UPD: check this question for my effort to generalize this approach. Using it, you can write
See the sources on Github.
This is a very readable and clear way to face that problem. First you should define this:
and later use
Using your problem as example you can do:
Inside the selfReferenced block you can use the outer object with no restrictions. The only thing you should take care of, is declaring the type explicitly to avoid recursive type checking issues.