context: I'm trying to make a function more generi...
# announcements
x
context: I'm trying to make a function more generic so it takes two types of message, here's code which might make it more clear:
s
please thread this
x
ok, sorry!
👍 1
Copy code
.let { objectMapper.readValue(it, E::class.java) }
                .let { objectMapper.readValue(it.message, T::class.java) }
previously E was not generic, but a normal class
and had a message property which was always a string
(called message), now I want message to be able to be either a String or an object itself
s
do you really need the function to be generic?
can’t you just accept your
Message
class and treat them the same?
you only really need generics if the type is important for correctness
x
sorry, what do you mean accept my message class? since objectMapper is expecting message to be a string
if it gets an object it breaks
s
what is the type of
it
x
originally it was a data class Message which looked like this:
Copy code
data class Message(
        @JsonProperty("Message")
        val message: String
    )
now i want message to be either a String or an object
s
I think you might be better served with an overload rather than trying to bend generics to work here
have one function that takes
Message
, another that takes
String
x
can you overload a data class?
ahh got you!
thank you for your help! that seems more intuitive!
👍 1