Hello. I have a class (let's call it `Entity`) tha...
# announcements
b
Hello. I have a class (let's call it
Entity
) that is inherited from two children, and they need to change the type of one property. This is because these classes are used to retrieve data from a backend (in serialized in JSON, deserialized with Gson). In fact, the server uses the same object for downlink (e.g.
GET /x/y/z.json
returns a list of
Entity
) and uplink (
POST /abc
with one
Entity
in the body), but one of the fields has not the same type whether it's uplink or downlink (bummer... I can't change the server code...). So in
Entity
i have declared the property as
open var stuff: Any? = null
, and I expected the
EntityUplink
for example to have
override var stuff: ActualType? = null
but the compiler refuses that. Is that a violation of the language (e.g. not possible/not designed to do that) or am I missing something?
h
Well, that's a consequence of the https://en.wikipedia.org/wiki/Liskov_substitution_principle If you have a class that deals with
Entity
objects, it expects to be able to put
Any
type in the
stuff
field. You can't have a subclass which restricts that.
b
Thanks a lot. Didn't know this WP article.
Actually the subclass doesn't restrict that, it's just that this field is retrieved from the local database, and my abstraction layer needs an explicit type to figure out how to convert from database type to kotlin type.
it could deal with
Any
but then I would need a workaround in the database code so that the DAO knows how to extract this
Any
from the database.
anyway this is no longer relevant, see main channel.