Kotlin Meta and CT programming vs D

Jo Blow JoBlow at dot.com
Sat Dec 21 19:20:40 UTC 2024


On Thursday, 19 December 2024 at 11:13:38 UTC, Jürgen Reichmann 
wrote:
> Hi Jo,
>
> metaprogramming in Kotlin is possible by writing compiler 
> plugins
> see https://kt.academy/article/ak-compiler-plugin
> or 
> https://resources.jetbrains.com/storage/products/kotlinconf2018/slides/5_Writing%20Your%20First%20Kotlin%20Compiler%20Plugin.pdf
>
> An alternative approach would be
> https://www.baeldung.com/kotlin/kotlinpoet
> "KotlinPoet is an open-source library that generates Kotlin 
> source code."
>
> Kind regards
> Jürgen

Yeah, I came across Poet when I was trying to see how to do 
metaprogramming. At the time I couldn't find anything and the 
link helps explain it but it's sorta like "whats the point". I 
can do that stuff just using strings. It might work out well for 
certain things(I imagine one can write a string mixin and stuff).

When I was trying to do anything with it the problem I was really 
having was that I needed to either generate or rewrite code. It 
seems Poet can't rewrite code but it can generate.

But the issue I was having was that to generate a piece of code I 
1. needed to access the project code so I could "reflect" on it. 
2. Add have the project then use that piece of code.

To generate the code I used a build task but then then it 
couldn't access the project code.  I'd post the build task but I 
can't seem to find it but basically it just ran the kotlin code I 
was using to generate the file but it needed a type from my 
project to reflect over which I couldn't figure out how to get it 
to be able to see. To solve that I just started calling it in my 
app rather than the task and essentially manually did the 
work(luckily I rarely have to mess with it).

So basically there is a dependency issue here: To generate the 
code I need access to the project after it was partially built up 
to that point then needed to include that generated code. I 
thought I would try putting the dependencies part in a new module 
but it became a mess real quick.

It might be my own ignorance of how everything works but I don't 
think things like KotlinPoet will solve that since they are just 
a structured way of building code. It will still have the 
problem. D just works in this case as it automatically takes care 
of these intra-dependencies without issue.





More information about the Digitalmars-d-learn mailing list