{"id":13605992,"url":"https://github.com/Karumi/WeakDelegate","last_synced_at":"2025-04-12T05:35:30.257Z","repository":{"id":58508324,"uuid":"114389902","full_name":"Karumi/WeakDelegate","owner":"Karumi","description":"WeakReference property delegate proposal","archived":false,"fork":false,"pushed_at":"2020-08-08T17:43:52.000Z","size":69,"stargazers_count":47,"open_issues_count":2,"forks_count":6,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-02T19:38:49.465Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Karumi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-15T16:22:56.000Z","updated_at":"2024-02-08T08:45:29.000Z","dependencies_parsed_at":"2022-09-06T12:50:56.788Z","dependency_job_id":null,"html_url":"https://github.com/Karumi/WeakDelegate","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Karumi%2FWeakDelegate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Karumi%2FWeakDelegate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Karumi%2FWeakDelegate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Karumi%2FWeakDelegate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Karumi","download_url":"https://codeload.github.com/Karumi/WeakDelegate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223498131,"owners_count":17155268,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T19:01:04.999Z","updated_at":"2024-11-07T10:31:40.605Z","avatar_url":"https://github.com/Karumi.png","language":"Kotlin","funding_links":[],"categories":["Kotlin"],"sub_categories":[],"readme":"# Weak Delegate [![](https://jitpack.io/v/Karumi/WeakDelegate.svg)](https://jitpack.io/#Karumi/WeakDelegate)\n\nThis project implements a Kotlin wrapper for the almighty (and sometimes scary) [weak references](https://docs.oracle.com/javase/7/docs/api/java/lang/ref/WeakReference.html) so that you can use them in a more friendly way.\n\n## Usage\n\nWeak references are often feared but are one of the most important tools Java and Kotlin developers have. If you are still unsure if you should use them just read one of the multiple resources linked below.\n\nHowever, if you ever tried to use a weak reference in kotlin you probably found yourself writing things like:\n\n```kotlin\nclass SomeClass {\n    private var wr: WeakReference\u003cData\u003e? = null\n\n    fun foo(data: Data) {\n        wr = WeakReference(data)\n    }\n\n    fun bar() {\n        val sr = wr?.get()\n\n        if (sr != null) {\n            print(sr)\n        }\n    }\n}\n```\n\nWould not it be better if we used the full potential of [delegated properties](https://kotlinlang.org/docs/reference/delegated-properties.html) for that matter? Weak Delegate creates one for you. Compare the previous example to the usage of Weak Delegate:\n\n```kotlin\nclass SomeClass {\n    private val weakRef: Data? by weak(Data(\"Some Data\"))\n    private var weakRefVar: Data? by weak()\n\n    fun set(data: Data) {\n        weakRefVar = data\n    }\n\n    fun bar() {\n        val strongRef = weakRef\n\n        if (strongRef != null) {\n            print(strongRef)\n        }\n    }\n}\n```\n\nNo more references to `WeakReference`s or `get` methods. Internally, Weak Delegate is doing the WeakReference \u003c-\u003e Nullable conversions for you.\n\n## Installation\n\nAdd the [jitpack](https://jitpack.io/) repository and include the WeakDelegate dependency to your `build.gradle` configuration:\n\n```groovy\nrepositories { \n     maven { url \"https://jitpack.io\" }\n}\ndependencies {\n      implementation 'com.github.Karumi:WeakDelegate:1.0.1'\n}\n```\n\n## References\n\n* [Understanding Weak References Blog](https://community.oracle.com/blogs/enicholas/2006/05/04/understanding-weak-references)\n* [Weak, Soft, and Phantom References in Java (and Why They Matter)](https://dzone.com/articles/weak-soft-and-phantom-references-in-java-and-why-they-matter)\n* [Java: difference between strong/soft/weak/phantom reference](https://stackoverflow.com/a/23519721/3959570)\n\nLicense\n-------\n\n    Copyright 2017 Karumi\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKarumi%2FWeakDelegate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKarumi%2FWeakDelegate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKarumi%2FWeakDelegate/lists"}