Object with no nontrivial superclasses


I’m trying to run a sample from the Kotlin reference.

This one works:
fun main(args: Array<String>) {
  val adHoc = object {
  var x : Int = 0

And this one not (has not been compiled because of unresolved reference adHoc.x):

val adHoc = object {   var x : Int = 0 }

fun main(args: Array<String>) {   print(adHoc.x) }

Why? What’s the difference?

Thanks, Alexey

This happens because of some internal troubles in teh compile, in short only locally defined object preserve precise types, because otherwise Java interop may break. In fact, we'll try to fix it in the future.

As a workaround, you can use a named object:

object adHoc {   var x : Int = 0 }