{"action":"created","issue":{"url":"https://api.github.com/repos/JetBrains/kotlin-native/issues/1817","repository_url":"https://api.github.com/repos/JetBrains/kotlin-native","labels_url":"https://api.github.com/repos/JetBrains/kotlin-native/issues/1817/labels{/name}","comments_url":"https://api.github.com/repos/JetBrains/kotlin-native/issues/1817/comments","events_url":"https://api.github.com/repos/JetBrains/kotlin-native/issues/1817/events","html_url":"https://github.com/JetBrains/kotlin-native/issues/1817","id":342328067,"node_id":"MDU6SXNzdWUzNDIzMjgwNjc=","number":1817,"title":"Laziness in frozen objects","user":{"login":"sandwwraith","id":11777575,"node_id":"MDQ6VXNlcjExNzc3NTc1","avatar_url":"https://avatars1.githubusercontent.com/u/11777575?v=4","gravatar_id":"","url":"https://api.github.com/users/sandwwraith","html_url":"https://github.com/sandwwraith","followers_url":"https://api.github.com/users/sandwwraith/followers","following_url":"https://api.github.com/users/sandwwraith/following{/other_user}","gists_url":"https://api.github.com/users/sandwwraith/gists{/gist_id}","starred_url":"https://api.github.com/users/sandwwraith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sandwwraith/subscriptions","organizations_url":"https://api.github.com/users/sandwwraith/orgs","repos_url":"https://api.github.com/users/sandwwraith/repos","events_url":"https://api.github.com/users/sandwwraith/events{/privacy}","received_events_url":"https://api.github.com/users/sandwwraith/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-07-18T13:26:59Z","updated_at":"2018-07-18T16:44:49Z","closed_at":null,"author_association":"MEMBER","body":"I think we need some sort of `by lazy {}` support even if the object is frozen. Use-case:\r\n\r\n```kotlin\r\ndata class Data(val a: Int) {\r\n        object serializer: KSerializer<Data> {\r\n        override val descriptor: SerialDescriptor\r\n\r\n        init {\r\n            val desc = SerialClassDescImpl(\"Data\")\r\n            desc.addElement(\"name\")\r\n            descriptor = desc\r\n        }\r\n        }\r\n}\r\n\r\nclass SerialClassDescImpl(val className: String) { \r\n// ...\r\n\r\nprivate val array: MutableList<String>\r\nfun addElement(name: String) = array.add(name)\r\nprivate val inverseIndices: Map<String, Int> by lazy { buildIndices() }\r\nfun buildIndices() = /** compute inverse index of [array] **/\r\n}\r\n```\r\n\r\nThis code works fine in Kotlin/JVM and Kotlin/JS, but it throws exception on Kotlin/Native, because `object serializer` got frozen after creation and `descriptor` is frozen too, while actual access to lazy indices is performed much later (in deserialization of class Data). Lazy structures in this place are important for performance of serialization framework. While this particular case can be workarounded by forcing all computations before assignment to object field, in general, absence of this feature can make impossible certain optimisations. @elizarov  supposes such lazy computation in frozen objects can be performed using atomic references.\r\n"},"comment":{"url":"https://api.github.com/repos/JetBrains/kotlin-native/issues/comments/405998357","html_url":"https://github.com/JetBrains/kotlin-native/issues/1817#issuecomment-405998357","issue_url":"https://api.github.com/repos/JetBrains/kotlin-native/issues/1817","id":405998357,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNTk5ODM1Nw==","user":{"login":"olonho","id":2600522,"node_id":"MDQ6VXNlcjI2MDA1MjI=","avatar_url":"https://avatars1.githubusercontent.com/u/2600522?v=4","gravatar_id":"","url":"https://api.github.com/users/olonho","html_url":"https://github.com/olonho","followers_url":"https://api.github.com/users/olonho/followers","following_url":"https://api.github.com/users/olonho/following{/other_user}","gists_url":"https://api.github.com/users/olonho/gists{/gist_id}","starred_url":"https://api.github.com/users/olonho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olonho/subscriptions","organizations_url":"https://api.github.com/users/olonho/orgs","repos_url":"https://api.github.com/users/olonho/repos","events_url":"https://api.github.com/users/olonho/events{/privacy}","received_events_url":"https://api.github.com/users/olonho/received_events","type":"User","site_admin":false},"created_at":"2018-07-18T16:44:49Z","updated_at":"2018-07-18T16:44:49Z","author_association":"CONTRIBUTOR","body":"`atomicLazy` freezes producer lamdba, so it cannot be used as drop in replacement for regular `lazy`."}}
{"actor":{"display_login":"olonho"}}