https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jonathan Hollingsworth

10/05/2021, 1:16 AM
Hello. I’ve got a Data Class I’m defining that can represent database columns. I’d like one of the properties on the Data Class to be
defaultValue
, but the type of that value is dependent on
dataType
property. I was thinking I’d need a union type for
defaultValue
, but just found out Kotlin doesn’t support that. I could use
Any
, but that breaks the kotlinx.serialization
@Serialiazable
annotation. Has anyone got any ideas or can point me in the right direction?
h

hho

10/05/2021, 1:56 AM
j

Jonathan Hollingsworth

10/05/2021, 2:09 AM
Hmm… thanks for that. In this case
dataType
is going to be a string representing a postgres data type, but I might be able to use that with a
mapOf
or something to determine the relevant type.
a

andylamax

10/05/2021, 7:15 AM
A bit away from the question, but this is not use case for a data class. You get a lot of overhead with no benefits at all. Just use a plain class instead for this
j

Jonathan Hollingsworth

10/05/2021, 7:20 AM
There are some benefits in the wider usage - which is basically acting as a way of deserializing JSON data from a client app into a known structure. I totally get the point though; I’m a Kotlin noob and might well be going about this the wrong way.
5 Views