# Code generate a couple of functions at compile-time — HOW?

**URL:** <https://discuss.kotlinlang.org/t/code-generate-a-couple-of-functions-at-compile-time-how/19912>\
**Category:** Uncategorized\
**Created:** [November 15, 2020, 7:18am UTC](https://discuss.kotlinlang.org/t/code-generate-a-couple-of-functions-at-compile-time-how/19912 "2020-11-15T07:18:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alamothe](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@alamothe](https://discuss.kotlinlang.org/u/alamothe)\
**Post date:** [November 15, 2020, 7:18am UTC](https://discuss.kotlinlang.org/t/code-generate-a-couple-of-functions-at-compile-time-how/19912/1 "2020-11-15T07:18:03Z")

</div>

Hello,  
I’m wondering what’s the current state of “compile-time code generation”. What I need is basically an annotation which, if applied, will generate a couple of boilerplate methods for that object/class.

For example:

```auto
@Hashcode
class Foo(
    val i: Int,
    val s: String,
    val b: Boolean)

```

This is just an example, I know that data classes have `hashCode` implementation already. But my case is very similar and I would like to generate a couple of boilerplate methods if the annotation is present.

Where are we currently? Is this supported? Where can I check for a minimal setup to make it work?

I suspect that Moshi and Kotlin Serialization are already doing something like this.

Thank you

---

<div class="post-metadata">

**Author:** ![bjonnh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bjonnh/32/8106_2.png) [@bjonnh](https://discuss.kotlinlang.org/u/bjonnh)\
**Post date:** [November 15, 2020, 3:23pm UTC](https://discuss.kotlinlang.org/t/code-generate-a-couple-of-functions-at-compile-time-how/19912/2 "2020-11-15T15:23:18Z")

</div>

Have a look at [Kotlin Poet](https://square.github.io/kotlinpoet/).

---

<div class="post-metadata">

**Author:** ![alamothe](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@alamothe](https://discuss.kotlinlang.org/u/alamothe)\
**Post date:** [November 18, 2020, 1:25am UTC](https://discuss.kotlinlang.org/t/code-generate-a-couple-of-functions-at-compile-time-how/19912/3 "2020-11-18T01:25:46Z")

</div>

Kotlin Poet seems like a useless templating engine which is exactly what I don’t need. I need generation at compile time.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [November 18, 2020, 1:45am UTC](https://discuss.kotlinlang.org/t/code-generate-a-couple-of-functions-at-compile-time-how/19912/4 "2020-11-18T01:45:06Z")

</div>

If you are targeting the JVM you should look into kapt which is the kotlin version of java anotation processing. If you need to add real member functions to classes or target mpp/js/native you have to use compiler plugins which is still an kotlin internal featuer. However the arrow guys have a framework that should allow you to write your own plugins anyways. Not sure how stable it is. Last time I experimented it (about a year ago) it was still in an early beta stage.
