is it possible to write a compiler that would, bas...
# compiler
e
is it possible to write a compiler that would, based on an annotation, say, add a nested type (or toplevel but nested would make more semantic sense in my case) and add synthetic methods to a class? an extension function would also work I suppose.
e
if by synthetic you mean JVM synthetic, that probably doesn't help you since those aren't callable from normal Java code
generating top-level extension functions is easy enough with ksp
e
yeah. synthetic would break down so that was probably a poor choice of words.
e
if you want to add a function to the class itself, that'll require a compiler plugin
e
right. would KSP work for that? (I've written a KSP plugin but it generates external sources rather than modify the processed source)
e
an extension function can be external
e
well, an extension function would still feel odd to a Java user, e.g.
otherwise, from kotlin, the defaulted parameters would work just fine.
i'm mostly concerned with smoothing over the API use from a Java project.
e
I made https://github.com/ephemient/builder-generator on KAPT ages ago, haven't maintained it but basically you could do the same thing with KSP
e
the builder is the easy part. i need that extra method that takes it as a parameter.
e
e
let me put together a gist of what I'm i'm thinking.
it's probably not perfect but something like that.
i could probably use the
allopen
and
noarg
plugins as inspiration...
e
since default parameters can depend on other parameters, I don't think you can expose them as `var`s in general, only setters
e
yeah, there are some wrinkles to work out for sure. i'm just trying to find the best place to try and get a foothold on it all.
e
the approach I took in my builder sample would result in
Copy code
new Blah_Foo_Builder(blah)
    .withAge(12)
    .build();
is it worth going through the extra effort to have the call look like
blah.foo(...)
from Java? well, I guess it's up to you
e
in my case, i'm not necessarily trying to build an object as I am trying to smooth over the use of default argument values on the (java) caller side. some i'm more focused on function invocation than object construction if that makes sense.
i.e., a builder is for constructor convenience where my idea is more generalized for functions at large.
e
sure the naming doesn't make sense for this case but that's a simple detail
e
yeah. once/if I can get a working plugin naming becomes paramount. 😄