has anyone successfully overridden `toString()` in...
# compiler
w
has anyone successfully overridden
toString()
in a data class or is the limitation stated here still a problem? https://github.com/ZacSweers/redacted-compiler-plugin
r
A compiler plugin can override the default toString implementation either in the ASM codegen or IR phase
that an any other impl for what is worth
For example here we modify value arguments of function calls in the IR tree https://github.com/arrow-kt/arrow-meta/blob/master/compiler-plugin/src/main/kotlin/arrow/meta/plugins/typeclasses/TypeClassesPlugin.kt#L60 before .class gen happens
j
https://github.com/ZacSweers/redacted-compiler-plugin/pull/2 Seems to be working fine. Only thing left is to add check if annotation is on class. And propably removal of
FINAL
modifier And (propably, but I'm not sure) check if result it not empty beforehand
So it must be false or non synthetised?
r
toString may be flagged as a fake override
w
oh wow! thanks for that mvp
👍 1
I'm super unfamiliar with compiler plugins so it'll take a while for me to digest that 😅
👍 1
r
fake overrides are a special markers for things like equals, hashcode etc. @wiyarmir what are you trying to do?
w
basically exactly what that plugin is doing but for data classes
r
You can do that with arrow meta and just quotes without bothering with descriptors or resolution
It's just replacing toString if it exists or adding it if it doesn't but only for classes you write
Are these your classes or third party?
w
ours
r
I'll send you an example at the end of the morning or if you want to pair I'll show you how to build something that does that
w
thanks! 🙇 don't stress too much about it, I'm still in exploratory phase wether this is the right solution
w
@raulraja so does IDE automatically work with all such compiler plugins, some compiler plugins, none? I’m curious if it requires e.g. an IDE plugin as well or whether IJ handles discovery, completions, navigation etc. automatically
r
Meta automatically creates the synth descriptors for IDEA if in your generated code you use
.synthetic
. In this case since toString is already synthetic it will just work as if the user wrote it
so this plugin requires no IDE integration
w
Are there any cases where IDE support isn’t there automatically? For example I’m interested in union types, but I’m much less likely to try them out if they require additional support in the IDE
r
for more complex plugin where you alter resolution or add members to existing user declarations you need the automatic synth resolver that comes with the Meta plugin. The good news is that all that can be autoinstalled
👍 1
w
If it’s all handled by Arrow API and plugin, this sounds amazing
r
so essentially as you add the Meta plugin to gradle it can copy the IDEA plugin in the user plugins folder
and then give you the IDE synth resolution
alternatively if Kotlin ever has proper macros or meta IDEA will automatically read the synth descriptors like it does with the Meta plugin
w
I’m more worried about IntelliJ vs AndroidStudio differences and early IJ preview support. I don’t doubt Arrow will be maintained, IDE plugin is just somewhat tricky sometimes
r
The purpose of meta is to eliminate all the boilerplate from writing compiler plugins including their IDE integration piece
Meta depends in the Kotlin plugin
w
alternatively if Kotlin ever has proper macros or meta IDEA
Are there any issues about this to track? Is it likely that something like this will come out anytime soon?
r
the API is the standard IDEA plugins Open API so it should work on AS
👍 2
w
Thanks 🙂 I’ve been wanting to try out Arrow for some time now, I should find some time eventually
r
I have no idea what the compiler team think about meta programming or compiler plugins as these were announced a long time ago but have been in use only for strategic development and no community support. I suppose they are busy with other important stuff and I don’t blame them as Kotlin is huge by now. This is the gap Meta tries to fill as Kotlin gets its metaprogramming story together. If it ever makes it to the lang we can get rid of the plugins or meta itself.
z
Sorry to necro this, but for anyone interested the original library is now published https://twitter.com/ZacSweers/status/1221907910000857089
❤️ 1