Kotlin compiler plugin that allows class delegation to be dynamic like property delegations

Overview

kotlin-dynamic-delegation

Kotlin compiler plugin that allows class delegation to be dynamic like property delegations.

The plugin is working in progress.

What does this plugin do

It provides a function

public fun <R> dynamicDelegation(value: () -> R): R =
    throw NotImplementedError("Implemented as intrinsic")

This function is implemented by the compiler. You can call this function on class delegation as follows:

interface TestClass {
    fun getResult(): Int
}
object TestObject : TestClass by (dynamicDelegation { getInstanceFromOtherPlaces() })

var called = 0
fun getInstanceFromOtherPlaces(): TestClass {
    val v = called++
    return object : TestClass {
        override fun getResult(): Int = v // static value here!
    }
}

class Test {
    @Test
    fun test() {
        assertEquals(0, TestObject.getResult())
        assertEquals(1, TestObject.getResult())
        assertEquals(2, TestObject.getResult())
    }
}

The compiler generates TestObject.getResult() as follows:

override fun getResult(): Int = getInstanceFromOtherPlaces().getResult()

So the delegated instance can be changed.

You might also like...
A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays

PneumoniaClassifier A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays. W

Go Outside is an augmented reality app designed for our team's Software Engineering UI/UX class
Go Outside is an augmented reality app designed for our team's Software Engineering UI/UX class

Go Outside Go Outside is an augmented reality app designed for our team's Software Engineering UI/UX class. This app serves as a PROTOTYPE to demonstr

PenPath class is designed to be used to draw paths with variable thickness
PenPath class is designed to be used to draw paths with variable thickness

PenPath class is designed to be used to draw paths with variable thickness. It creates an object of android.graphics.Path and should be drawn with Paint.Style.FILL type paint.

Simplify the processing of sealed class/interface

shiirudo Generates DSL to simplify processing branching by when expressions in sealed class/interface. Setup Refer to the KSP quickstart guide to make

A tool to model and analyze the design of systems from .class files
A tool to model and analyze the design of systems from .class files

sift A tool to model and analyze the design of systems from java class files. Features CLI tool for building, querying and diff-ing "system models" fr

Gradle plugin which allows to use typed DSL for generating kubernetes/openshift YAML files

gr8s Gradle plugin which allows using typed DSL for generating kubernetes/openshift YAML files. Based on kuberig Usage import io.github.guai.gr8s.Gene

A simple plugin to patch the memory leak in Kotlin Gradle Plugin 1.5.0

kgp-150-leak-patcher A simple plugin to automatically patch the memory leak in Kotlin Gradle Plugin 1.5.0 in https://youtrack.jetbrains.com/issue/KT-4

The tool allows to dump binary API of a Kotlin library

Binary compatibility validator The tool allows to dump binary API of a Kotlin library that is public in sense of Kotlin visibilities and ensures that

KVision allows you to build modern web applications with the Kotlin language

KVision allows you to build modern web applications with the Kotlin language, without any use of HTML, CSS or JavaScript. It gives you a rich hierarchy of ready to use GUI components, which can be used as builder blocks for the application UI.

Releases(v0.3.0-170.1)
  • v0.3.0-170.1(May 27, 2022)

  • v0.3.0-162.4(Apr 26, 2022)

  • v0.2.0-160.1(Jan 17, 2022)

    • Support dynamic delegations by property reference with receivers:
    package me.him188.kotlin.dynamic.delegation
    
    /**
     * Creates a dynamic delegation.
     *
     * Type [T] must be the same as, or a supertype of, the class that uses `dynamicDelegation`.
     *
     * Example of usage:
     * ```
     * internal class CombinedMessage(
     *     val element: Message,
     *     val tail: Message
     * ) : MessageChain, List<SingleMessage> by dynamicDelegation(CombinedMessage::refined) {
     *     val refined: List<SingleMessage> by lazy {
     *         // lazily initialize the List<SingleMessage>
     *     }
     * }
     * ```
     */
    public fun <T, R> dynamicDelegation(reference: KProperty1<T, R>): R =
        throw NotImplementedError("Implemented as intrinsic")
    

    This version of the compiler plugin supports Kotlin versions:

    • 1.6.0-M1
    • 1.6.0-RC
    • 1.6.0
    • 1.6.10-RC
    • 1.6.10
    Source code(tar.gz)
    Source code(zip)
  • v0.1.1-160.1(Dec 26, 2021)

  • v0.1.0-160.1(Dec 23, 2021)

Owner
Him188
Undergraduate at Imperial. Kyoani forever.
Him188
Maxibon kata for Kotlin Developers. The main goal is to practice property based testing.

Kata Maxibon for Kotlin. We are here to practice property based testing. We are going to use KotlinTest to write our tests. We are going to practice p

Karumi 44 Oct 3, 2022
Example project for using the Selenium toolkit with Kotlin, Maven, TestNg and the config is managed via a property file.

Selenium-Java-Toolkit-TestNg-Playground This is the sample-Project and show you how to use the Selenium-Toolkit. The Selenium-Toolkit is a Java based

Selenium Consulting 0 Dec 15, 2021
Kotlin compiler plugin for converting suspend functions to platform-compatible functions

Kotlin suspend transform compiler plugin Summary Kotlin compiler plugin for generating platform-compatible functions for suspend functions. JVM class

ForteScarlet 5 Oct 12, 2022
glide's ksp compiler ,use kotlin symbol processor

glide-ksp glide's ksp compiler ,use kotlin symbol processor requirements library version kotlin >= 1.6.10 ksp 1.6.10-1.0.2 usage add jitpack repositor

Mistletoe 24 Oct 17, 2022
FragmentContainerViewIdBugDemo - minimal repro project demonstrating a bug in FragmentContainerView's id check logic in the context of a dynamic feature module

FragmentContainerViewIdBugDemo minimal reproduce project demonstrating an apparent bug in FragmentContainerView's id check logic in the context of a d

null 0 Jan 5, 2022
Kotlin & Java class for gRPC client

jvm-minter-grpc-class Kotlin & Java class for gRPC client Use @TODO Разное Добавление git subtree add --prefix node-grpc-gateway https://github.com/Mi

Aleksey Kukhnovets 2 May 26, 2022
A simple Kotlin class to use to connect to a MongoDB collection.

mongodb-kotlin A simple Kotlin class to use to connect to a MongoDB collection. Once you have created a MongoDB project, choose Build a Database: Next

null 2 Oct 23, 2022
Kotlin Example of how to organize your code using MVC and some patterns seen in class

Kotlin Example of how to organize your code using MVC and some patterns seen in class

José Luis González Sánchez 3 Mar 23, 2022
Minimalistic class proxy creator for Kotlin

reflektion Minimalistic class proxy creator for Kotlin. Reflektion allows you to create an implementation of an interface you provide containing proxy

Subham 6 Nov 11, 2022
BindsAdapter is an Android library to help you create and maintain Adapter class easier via ksp( Kotlin Symbol Processing).

BindsAdapter BindsAdapter is an Android library to help you create and maintain Adapter class easier via ksp( Kotlin Symbol Processing). Installation

Jintin 5 Jul 30, 2022