6

Is it somehow possible to have "annotation aliases" in Kotlin?

What I want to achieve is to transform this

@Colour("red")
fun method() = "something"

into something like this

annotationalias Red = Colour("red")

@Red
fun method() = "something"
Federico Bellini
  • 365
  • 1
  • 7
  • 18
  • 1
    That would be a cool feature, but it is currently not in Kotlin. You need to write your own annotation called `Red`, and write your own annotation processor that translates it into `Colour("red")`. – Sweeper Jun 09 '22 at 10:48
  • If you have control over the processor that's reading the annotations, you can change it to search for aliases manually. Some libraries already include this, like Spring Boot's [meta-annotations and composed annotations](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-meta-annotations). – aSemy Jun 09 '22 at 17:18
  • See also https://stackoverflow.com/q/55711084/4161471 – aSemy Jun 09 '22 at 21:35

0 Answers0