Kotlin Annotation Processing: is it possible to generate an extension method?

hotkey

I'm playing around with Kotlin and I think that it would be great to have "functional classes", see the example of what I want:

functional class MyStrFunction(val function: MyStrFunction.(String) -> String) {
    val memory = HashMap<String, String>()
    fun output(message: String) = println(message)
}

The idea is that I would be able to use it like:

val f = MyStrFunction { 
    output("reversing $it"); 
    if (it in memory)
        output(memory[it])
    return it.reverse() 
}

If I were to write MyStrFunction which I want by myself, the resulting class would be:

class MyStrFunction(val function: MyStrFunction.(String) -> String) {
    val memory = HashMap<String, String>()
    fun output(message: String) = println(message)

    public fun invoke(s: String) = this.function(s)
}

But it's tedious a bit to write this by hands in every "functional class".

So here's my question: is it possible to make functional an annotation and then process it adding invoke method with the desired code?

I know that kapt can be used for Kotlin annotation processing, but AFAIK for now it cannot generate Kotlin code and there's no way to make an extension function in Java generated code.

Is there any way to generate a new Kotlin method for a class?

Node: I understand that the idea of functional classes has few use cases and that it's not that hard to add the mentioned invoke method manually, but for me it's academical interest to the concept of annotation processing in Kotlin.

UPD: Just my thought about functional classes: there's language feature of delegating interfaces and it would work good here, but this reference is not visible in that block. Example:

class MyStrFunction(val function: MyStrFunction.(String) -> String)
: (String) -> String by { this.function(it) } { // <-- won't compile :(
    val memory = HashMap<String, String>()
    fun output(message: String) = println(message)
}
Kirill Rakhman

Not sure, if what you want can be done directly, however here's an approach without "functional classes". As far as I understand you want to add functionality to a regular lambda function. First, define your functionality as a regular class:

class Memory() {
    val memory = hashMapOf<String, String>()
    fun output(message: String) = println(message)
}

Then define a helper function to bind extension functions to a receiver:

fun <T, R, E> T.bind(function: T.(R) -> E): (R) -> E = { this.function(it) }

And then use it like this

fun main(args: Array<String>) {
    val mem = Memory()

    val memoryFunction: Memory.(String) -> String = {
        output("reversing $it");
        if (it in memory)
            output(memory[it])
        it.reverse()
    }

    val function: (String) -> String = mem.bind(memoryFunction)

    println(listOf("foo", "bar").map(function))
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generate unit test using annotation processing

From Dev

Kotlin annotation processing ignores items with similar names

From Dev

Kotlin - Restrict extension method scope

From Dev

Is it possible to pass method parameter to annotation on a method?

From Dev

Use Static Method in Java as Extension Method in Kotlin

From Dev

Compile error calling an extension method from another extension method in Kotlin

From Dev

Compile error calling an extension method from another extension method in Kotlin

From Dev

IQueryable extension method to generate a SelectListItem list

From Dev

How can I refer to implementations of a method in annotation processing?

From Dev

How can I refer to implementations of a method in annotation processing?

From Dev

Is it possible to create an extension method using ReSharper?

From Dev

Is it possible to use .AsParallel Extension Method in PCL?

From Dev

Is it possible to write an extension method for a generic Enum type?

From Dev

Is it possible to link Java method from Kotlin comment?

From Dev

Is it possible to implement method with signature List<Class<? extends Annotation>> in Java?

From Dev

Can I create a Kotlin extension method for adding an rxJava Subscription to a CompositeSubscription?

From Dev

Java - Annotation Processing

From Dev

Lombok Requires Annotation Processing

From Dev

Is it possible to create an extension method that is called when the object is created?

From Dev

How can I get spock to execute a different method at runtime using an Annotation Extension?

From Dev

Is there an extension to beautify a Processing sketch?

From Dev

Is it possible to override Java getter (method) with Kotlin val (property)?

From Dev

Is it possible to create object instance through method reference in Kotlin

From Dev

Is it possible to generate C# files using protobuf-net for proto2 files with extension of 'FieldOptions'?

From Dev

kotlin and @Valid Spring annotation

From Dev

Override annotation required and not - Kotlin

From Dev

Kotlin custom annotation, arguments

From Dev

bug in kotlin annotation?

From Java

Android multi modules annotation processing

Related Related

  1. 1

    Generate unit test using annotation processing

  2. 2

    Kotlin annotation processing ignores items with similar names

  3. 3

    Kotlin - Restrict extension method scope

  4. 4

    Is it possible to pass method parameter to annotation on a method?

  5. 5

    Use Static Method in Java as Extension Method in Kotlin

  6. 6

    Compile error calling an extension method from another extension method in Kotlin

  7. 7

    Compile error calling an extension method from another extension method in Kotlin

  8. 8

    IQueryable extension method to generate a SelectListItem list

  9. 9

    How can I refer to implementations of a method in annotation processing?

  10. 10

    How can I refer to implementations of a method in annotation processing?

  11. 11

    Is it possible to create an extension method using ReSharper?

  12. 12

    Is it possible to use .AsParallel Extension Method in PCL?

  13. 13

    Is it possible to write an extension method for a generic Enum type?

  14. 14

    Is it possible to link Java method from Kotlin comment?

  15. 15

    Is it possible to implement method with signature List<Class<? extends Annotation>> in Java?

  16. 16

    Can I create a Kotlin extension method for adding an rxJava Subscription to a CompositeSubscription?

  17. 17

    Java - Annotation Processing

  18. 18

    Lombok Requires Annotation Processing

  19. 19

    Is it possible to create an extension method that is called when the object is created?

  20. 20

    How can I get spock to execute a different method at runtime using an Annotation Extension?

  21. 21

    Is there an extension to beautify a Processing sketch?

  22. 22

    Is it possible to override Java getter (method) with Kotlin val (property)?

  23. 23

    Is it possible to create object instance through method reference in Kotlin

  24. 24

    Is it possible to generate C# files using protobuf-net for proto2 files with extension of 'FieldOptions'?

  25. 25

    kotlin and @Valid Spring annotation

  26. 26

    Override annotation required and not - Kotlin

  27. 27

    Kotlin custom annotation, arguments

  28. 28

    bug in kotlin annotation?

  29. 29

    Android multi modules annotation processing

HotTag

Archive