Hello. I’ve got a Data Class I’m defining that can...
# getting-started
j
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
j
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
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
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.