Could someone please explain this difference in be...
# kotlin-native
a
Could someone please explain this difference in behavior between JVM and KN (iOS) (see snippet)? Called on JVM like (Kotlin - calling multiplatform library included as gradle dependency):
Copy code
println("State Test")
println("State Random inherited instance val 1: ${StateTest.InheritingSingleton.randomInstanceVal}")
println("State Random inherited instance val 2: ${StateTest.InheritingSingleton.randomInstanceVal}")
println("State Random inherited instance lazy 1: ${StateTest.InheritingSingleton.randomInstanceLazy}")
println("State Random inherited instance lazy 2: ${StateTest.InheritingSingleton.randomInstanceLazy}")
println("State Random file val 1: ${StateTest.InheritingSingleton.randomFileLevelVal}")
println("State Random file val 2: ${StateTest.InheritingSingleton.randomFileLevelVal}")
println("State Random file lazy 1: ${StateTest.InheritingSingleton.randomFileLevelLazy}")
println("State Random file lazy 2: ${StateTest.InheritingSingleton.randomFileLevelLazy}")
println("State Random independent val 1: ${StateTest.IndependentSingleton.randomSingletonSimple}")
println("State Random independent val 2: ${StateTest.IndependentSingleton.randomSingletonSimple}")
println("State Random independent lazy 1: ${StateTest.IndependentSingleton.randomSingletonLazy}")
println("State Random independent lazy 2: ${StateTest.IndependentSingleton.randomSingletonLazy}")
Called on iOS like (Swift - calling multiplatform library included as Cocoapod):
Copy code
print("State Test")
print("State Random inherited instance val 1: \(StateTest.InheritingSingleton().randomInstanceVal)")
print("State Random inherited instance val 2: \(StateTest.InheritingSingleton().randomInstanceVal)")
print("State Random inherited instance lazy 1: \(StateTest.InheritingSingleton().randomInstanceLazy)")
print("State Random inherited instance lazy 2: \(StateTest.InheritingSingleton().randomInstanceLazy)")
print("State Random file val 1: \(StateTest.InheritingSingleton().randomFileLevelVal)")
print("State Random file val 2: \(StateTest.InheritingSingleton().randomFileLevelVal)")
print("State Random file lazy 1: \(StateTest.InheritingSingleton().randomFileLevelLazy)")
print("State Random file lazy 2: \(StateTest.InheritingSingleton().randomFileLevelLazy)")
print("State Random independent val 1: \(StateTest.IndependentSingleton().randomSingletonSimple)")
print("State Random independent val 2: \(StateTest.IndependentSingleton().randomSingletonSimple)")
print("State Random independent lazy 1: \(StateTest.IndependentSingleton().randomSingletonLazy)")
print("State Random independent lazy 2: \(StateTest.IndependentSingleton().randomSingletonLazy)")
Output on JVM:
Copy code
State Test
StateTest init
StateTest.InheritingSingleton init
State Random inherited instance val 1: -14155247
State Random inherited instance val 2: -14155247
State Random inherited instance lazy 1: -1793673803
State Random inherited instance lazy 2: -1793673803
State Random file val 1: -483920171
State Random file val 2: -483920171
State Random file lazy 1: -1570839155
State Random file lazy 2: -1570839155
StateTest.IndependentSingleton init
State Random independent val 1: -883005959
State Random independent val 2: -883005959
State Random independent lazy 1: -1287948357
State Random independent lazy 2: -1287948357
Output on iOS:
Copy code
State Test
StateTest init
State Random inherited instance val 1: 137897944
StateTest init
State Random inherited instance val 2: -1206959235
StateTest init
State Random inherited instance lazy 1: -695365290
StateTest init
State Random inherited instance lazy 2: 1274463100
StateTest init
State Random file val 1: -525097753
StateTest init
State Random file val 2: -525097753
StateTest init
State Random file lazy 1: -549146703
StateTest init
State Random file lazy 2: -549146703
StateTest.IndependentSingleton init
State Random independent val 1: 1573861028
State Random independent val 2: 1573861028
State Random independent lazy 1: -1005578215
State Random independent lazy 2: -1005578215
Why is
StateTest init
called multiple times on iOS? As a result, the vals that I expect to be the same are also different? Why is
StateTest.InheritingSingleton init
never called at all?
p
I faced this in a multi threaded scenario and that was caused because each thread tries to create its own instance of the class.
a
@Prateek Grover thought it might be something like that but all the Swift lines above are called sequentially one immediately after another in the same (main) thread
p
yeah, i understand that. Unfortunately I have no explanation for this. Lets see what other members of the community has to say about this behaviour
o
the code you provided doesn’t have enough context to answer your question. Please open an issue and include complete reproducer and description of desired behavior.
a
@olonho I modified the cocoapods sample, forked from the master branch of
kotlin-native
, and added the code I posted above. The exact same behavior is then exhibited with a fresh checkout (so no other code from my proejct). I created an issue here detailing how this behavior differs from what I expect: https://github.com/JetBrains/kotlin-native/issues/3525 Is that sufficient?
👍 1