is there a way to create an extension function for...
# announcements
j
is there a way to create an extension function for an annotated data class?
k
Not really, neither "annotated" nor "data class" are represented by the type system.
s
yeah, data classes don’t inherit from anything but
Any
or implement any kind of interface by default, just like any other class
t
Theoretically you could generate them using an annotation processor
j
Thanks for the help. Is there a way to create an extension function on "Any" that has been annotated in some way?
s
you can annotate a lot of things, but based on this conversation I don’t think it’ll necessarily do what you want it to
d
Consider using an interface instead of an annotation.
👆 3
j
Thanks. Maybe I should elaborate a bit more on my problem. So I am using kotlin's Json serializer to convert my data classes to a json string. I have code that looks like this sprinkled all throughout my code base (Json(JsonConfiguration.Stable).stringify(myType.serializer(), myType(a, b, c)) . I thought it would be nice to just create an extension function on my data class where I can do: myType(a,b,c).toJson()
s
Would you mind clarifying what you mean by “kotlin’s JSON serializer?”
there are a number of them usable from Kotlin is all 😅
j
hah, sure 🙂