Description
Steps to reproduce:
- Check out this single-commit fork of master, modifying the Cocoapods sample:
AndreasMattsson@dc70a8c - Run the ios app and observe the printed output.
e.g.
State Test
StateTest init
State Random inherited instance val 1: 486183893
StateTest init
State Random inherited instance val 2: -229446675
StateTest init
State Random inherited instance lazy 1: -1434534466
StateTest init
State Random inherited instance lazy 2: 1847758197
StateTest init
State Random file val 1: -1751854446
StateTest init
State Random file val 2: -1751854446
StateTest init
State Random file lazy 1: 1950481871
StateTest init
State Random file lazy 2: 1950481871
StateTest.IndependentSingleton init
State Random independent val 1: -1186721552
State Random independent val 2: -1186721552
State Random independent lazy 1: -1858185365
State Random independent lazy 2: -1858185365
The differs from the expected behavior in that StateTest init is apparently run once for each access of InheritingSingleton
rather than providing singleton-like access. It also appears that the init block of InheritingSingleton
itself is never called. I would expect all pairwise ("1: " / "2:" ) logs here to print the same value, but this breaks down for the case of the object that inherits from StateTest.
For comparison, these are log outputs from doing equivalent calls on JVM:
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
Here the StateTest init block is only called once (multiple times on iOS). InheritingSingleton init is also called once (never on iOS). And the pairwise logs all show the same numbers (showing that the random number is only generated once for each val).