4 ms·
Kotlin has this amazing concept called compiler plugins where you can extend, intercept and alter the compiler at any stage of the compilation pipeline (ast, ps
by The_rationalist 6y ago
Kotlin has this amazing concept called compiler plugins where you can extend, intercept and alter the compiler at any stage of the compilation pipeline (ast, psi, ir) through an API.
There even are frameworks that make writing such plugins easier such as
https://github.com/arrow-kt/arrow-meta https://github.com/arrow-kt/arrow-meta
(which enable to bring union type, higher kinded types, pattern matching, etc)
Or https://github.com/google/ksp https://github.com/google/ksp
- brabel 6y agoWhile this is cool, it doesn't look like something very different from other existing JVM compiler plugins... it seems quite similar to Javac plugins[0] and perhaps somewhere in between that and Groovy AST transformers[1]. Manifold[2] is basically a javac plugin if I'm not mistaken, and it shows just how powerful stuff can be accomplished with this approach (including e.g. auto-generation of Java types from common modelling languages like GraphQL, JSON, YAML etc. adds extension methods, unit expressions, ranges, C++-like preprocessors, structural subtyping, even crazy stuff like type-safe reflection - like Ceylon used to have!). For Groovy, besides the built-in AST annotations like `@Immutable` and `@Canonical` (which turns a class into a data class, basically), you can achieve stuff like the Spock Testing Framework[3] which changes the Groovy syntax a little bit to make it really awesome for testing. [0] https://www.baeldung.com/java-build-compiler-plugin https://www.baeldung.com/java-build-compiler-plugin [1] https://groovy-lang.org/metaprogramming.html https://groovy-lang.org/metaprogramming.html [2] http://manifold.systems/ http://manifold.systems/ [3] https://spockframework.org/spock/docs/1.0/spock_primer.html https://spockframework.org/spock/docs/1.0/spock_primer.html
- shock 6y ago> crazy stuff like type-safe reflection - like Ceylon used to have I regret so much losing Ceylon :(
- The_rationalist 6y agoInteresting! However I wonder if building Java plugins is harder. For example: JPA entities have for requirement to have a no-arg constructor. Both Java records and Kotlin data classes have not such a constructor by default. However kotlin has a no arg compiler plugin that autogenerate it for data classes (without any annotation) and Java Records don't have anything similar yet to my knowledge. (however they would still remain incompatible with jpa anyway because unfortunately they are immutable)
- The_rationalist 6y agoManifold is very interesting. There is also a tail call recursion optimizer at the bytecode level https://github.com/Sipkab/jvm-tail-recursion https://github.com/Sipkab/jvm-tail-recursion It would be nice to have a list of such extensions of the language/bytecode
- specialist 6y agoThanks for mentioning this. Not knowing Kotlin, I'll read the links you provided. Guessing this is a bit like Java's annotations. FWIW, I'm not a fan Java's annotation processing plugin stuff. It's fun for personal projects, cuz metaprogramming FTW. But debugging someone else's custom annotation stuff really sucks.