{"id":16855155,"url":"https://github.com/him188/kotlin-dynamic-delegation","last_synced_at":"2025-08-22T13:45:16.457Z","repository":{"id":57736575,"uuid":"433969532","full_name":"Him188/kotlin-dynamic-delegation","owner":"Him188","description":"Kotlin compiler plugin that allows class delegation to be dynamic like property delegations","archived":false,"fork":false,"pushed_at":"2023-03-19T16:25:19.000Z","size":267,"stargazers_count":40,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-28T00:38:45.380Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/Him188.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-01T20:08:05.000Z","updated_at":"2025-06-14T07:29:18.000Z","dependencies_parsed_at":"2024-10-28T17:46:00.463Z","dependency_job_id":null,"html_url":"https://github.com/Him188/kotlin-dynamic-delegation","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/Him188/kotlin-dynamic-delegation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Him188%2Fkotlin-dynamic-delegation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Him188%2Fkotlin-dynamic-delegation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Him188%2Fkotlin-dynamic-delegation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Him188%2Fkotlin-dynamic-delegation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Him188","download_url":"https://codeload.github.com/Him188/kotlin-dynamic-delegation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Him188%2Fkotlin-dynamic-delegation/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262699002,"owners_count":23350222,"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-10-13T13:58:38.082Z","updated_at":"2025-06-30T02:33:30.129Z","avatar_url":"https://github.com/Him188.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kotlin-dynamic-delegation\n\nKotlin compiler plugin that allows class delegation to be dynamic like property delegations.\n\n## Features\n\n### Dynamic Delegations\n\nThe plugin provides:\n\n```kotlin\npublic fun \u003cR\u003e dynamicDelegation(value: () -\u003e R): R =\n    throw NotImplementedError(\"Implemented as intrinsic\")\n```\n\nThis function is implemented by the compiler. Here is an example.\n\nSuppose this is your existing code and is published to the public:\n\n```kotlin\ninterface CommandManager {\n    val commands: List\u003cCommand\u003e\n    fun register(command: Command)\n\n    companion object : CommandManager {\n        // implement CommandManager\n    }\n}\n```\n\nSuppose then you realized the CommandManager instance should not be static (statically initialized or singleton).\nYou changed the CommandManager to this because you cannot remove code that have been published to the public:\n\n```kotlin\ninterface CommandManager {\n    val commands: List\u003cCommand\u003e\n    fun register(command: Command)\n\n    companion object : CommandManager {\n        private val instance by lazy { CommandManagerImpl() }\n\n        // You must manually delegate all calls to instance\n        override val commands: List\u003cCommand\u003e get() = instance.commands\n        override fun register(command: Command): Unit = instance.register(command)\n    }\n}\n```\n\nNow with Kotlin Dynamic Delegation, you can do instead:\n\n```kotlin\ninterface CommandManager {\n    val commands: List\u003cCommand\u003e\n    fun register(command: Command)\n\n    companion object : CommandManager by (dynamicDelegation(Companion::instance)) {\n        private val instance by lazy { CommandManagerImpl() }\n\n        // All delegates will be generated automatically\n    }\n}\n```\n\nAlternatively, it's also possible to do this:\n\n```kotlin\ninterface CommandManager {\n    val commands: List\u003cCommand\u003e\n    fun register(command: Command)\n\n    companion object : CommandManager by (dynamicDelegation { getCommandManagerImpl() }) {\n        // All delegates will be generated automatically\n    }\n}\n\nprivate fun getCommandManagerImpl(): CommandManager {\n    // This is called to get actual CommandManager instance when a member inside CommandManager.Companion is called.\n\n    if (!someChecks()) {\n        error(\"It's not the time to call CommandManager!\")\n    }\n    if (someOtherChecks()) {\n        currentInstance = CommandManagerImpl() // Possible to update delegated instance\n    }\n    return currentInstance\n}\nprivate var currentInstance: CommandManager = CommandManagerImpl()\n```\n\n### Using Lazy In Functions\n\nFunctions can also be 'lazy' by making a value lazy and persistent.\n\n```kotlin\noverride fun toString(): String = persistent { this.joinToString() } // initialize lazily once and use afterwards.\n```\n\nis the same as\n\n```kotlin\nprivate val toStringTemp by lazy { this.joinToString() }\noverride fun toString(): String = toStringTemp\n```\n\n## Using the plugin\n\nbuild.gradle.kts\n\n```kotlin\nplugins {\n    id(\"me.him188.kotlin-dynamic-delegation\") version \"VERSION\"\n}\n```\n\nYou also need to add a 'compileOnly' dependency 'me.him188:kotlin-dynamic-delegation-runtime':\n\nFor JVM projects:\n\n```kotlin\ndependencies {\n    implementation(\"me.him188:kotlin-dynamic-delegation:VERSION\")\n}\n```\n\nFor MPP, adding to `commonMain` would automatically add for all targets:\n\n```kotlin\nkotlin.sourceSets {\n    commonMain {\n        dependencies {\n            implementation(\"me.him188:kotlin-dynamic-delegation:VERSION\")\n        }\n    }\n}\n```\n\nSee VERSION from [releases](https://github.com/Him188/kotlin-dynamic-delegation/releases)\n\n## Installing IntelliJ IDEA plugin\n\nIDEA plugin can help to edit code and to report errors before compiling the code. By seeing the type cast hint(see\npicture below), you will know, without compiling, that your code with dynamic delegations will work.\n\nPlugin Marketplace Page: https://plugins.jetbrains.com/plugin/18219-kotlin-dynamic-delegation\n\n![](.README_images/57c295e7.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhim188%2Fkotlin-dynamic-delegation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhim188%2Fkotlin-dynamic-delegation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhim188%2Fkotlin-dynamic-delegation/lists"}