https://kotlinlang.org logo
#announcements
Title
# announcements
j

Jon

08/07/2019, 11:14 PM
is there a way to create an extension function for an annotated data class?
k

karelpeeters

08/07/2019, 11:16 PM
Not really, neither "annotated" nor "data class" are represented by the type system.
s

Shawn

08/07/2019, 11:17 PM
yeah, data classes don’t inherit from anything but
Any
or implement any kind of interface by default, just like any other class
t

tschuchort

08/07/2019, 11:22 PM
Theoretically you could generate them using an annotation processor
j

Jon

08/08/2019, 12:09 AM
Thanks for the help. Is there a way to create an extension function on "Any" that has been annotated in some way?
s

Shawn

08/08/2019, 12:11 AM
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

Dominaezzz

08/08/2019, 12:12 AM
Consider using an interface instead of an annotation.
👆 3
j

Jon

08/08/2019, 1:22 AM
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

Shawn

08/08/2019, 1:53 AM
Would you mind clarifying what you mean by “kotlin’s JSON serializer?”
there are a number of them usable from Kotlin is all 😅
j

Jon

08/08/2019, 1:55 AM
hah, sure 🙂
2 Views