https://kotlinlang.org logo
#compiler
Title
# compiler
w

wiyarmir

11/04/2019, 4:24 PM
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

raulraja

11/04/2019, 6:14 PM
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

jereksel

11/04/2019, 6:37 PM
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

raulraja

11/04/2019, 9:32 PM
toString may be flagged as a fake override
w

wiyarmir

11/05/2019, 9:43 AM
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

raulraja

11/05/2019, 9:47 AM
fake overrides are a special markers for things like equals, hashcode etc. @wiyarmir what are you trying to do?
w

wiyarmir

11/05/2019, 9:47 AM
basically exactly what that plugin is doing but for data classes
r

raulraja

11/05/2019, 9:51 AM
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

wiyarmir

11/05/2019, 9:55 AM
ours
r

raulraja

11/05/2019, 9:58 AM
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

wiyarmir

11/05/2019, 10:24 AM
thanks! 🙇 don't stress too much about it, I'm still in exploratory phase wether this is the right solution
w

wasyl

11/05/2019, 12:56 PM
@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

raulraja

11/05/2019, 12:58 PM
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

wasyl

11/05/2019, 12:59 PM
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

raulraja

11/05/2019, 12:59 PM
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

wasyl

11/05/2019, 12:59 PM
If it’s all handled by Arrow API and plugin, this sounds amazing
r

raulraja

11/05/2019, 1:00 PM
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

wasyl

11/05/2019, 1:01 PM
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

raulraja

11/05/2019, 1:01 PM
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

wasyl

11/05/2019, 1:02 PM
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

raulraja

11/05/2019, 1:02 PM
the API is the standard IDEA plugins Open API so it should work on AS
👍 2
w

wasyl

11/05/2019, 1:04 PM
Thanks 🙂 I’ve been wanting to try out Arrow for some time now, I should find some time eventually
r

raulraja

11/05/2019, 1:04 PM
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

Zac Sweers

01/29/2020, 7:28 AM
Sorry to necro this, but for anyone interested the original library is now published https://twitter.com/ZacSweers/status/1221907910000857089
❤️ 1
9 Views