You are currently viewing I think I found a compiler bug? No clue why this happens

I think I found a compiler bug? No clue why this happens

By all accounts, neither of these values should be null, yet referencing the same object in a top-level value makes the other value magically become null. live “`kotlin open class Superclass { // cannot be interface object Subclass: Superclass() // must be object, must be declared inside superclass object Subclass2: Superclass() // this order doesn’t matter

companion object { // cannot be named non-companion object val whyIsThisNull = Subclass // may be set in companion object's init {} val whyIsThisNotNull = Subclass2 // this order also doesn't matter } 

}

// must be top-level, order does matter val someValue = Superclass.Subclass val anotherValue = Superclass.Subclass2

fun main() { println(Superclass.whyIsThisNull == null) // “true” println(Superclass.whyIsThisNotNull == null) // “false” // this doesn’t happen on JS legacy or IR targets } “`

submitted by /u/YoshiRulz
[link] [comments]