-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support polymorphism #103
Comments
Seems like the first example doesn't compile as the open class Animal(open var type: String)
data class Dog(override var type: String, var name: String): Animal(type=type)
data class Cat(override var type: String, var size: Int): Animal(type=type) Anyway this sounds like the easier approach to follow, at least for now. Having |
Hi guys, what about using interfaces as the supertype? interface Animal(var type: String)
data class Dog(override var type: String, var name: String): Animal
data class Cat(override var type: String, var size: Int): Animal Or sealed interface Animal(var type: String)
data class Dog(override var type: String, var name: String): Animal
data class Cat(override var type: String, var size: Int): Animal (Dog and Cat can be nested inside Animal or not in this case. I would almost always prefer it to not be nested, but this is of course totally discussable) I feel like interfaces work better with data classes and are exactly what we need for this. I have used this approach in big inheritance hierarchies with data classes where every inflection point is modeled with an interface and it has worked great for me.
|
Agree. When we initally discussed the Poly support, |
Swagger specifications allow models to be polymorphic via a
discriminator
field (doc).This allows a model, let's say
Animal
to be a sort of parent in the hierarchy of other types likeDog
,Cat
, etc.The polymorphic approach is generally interesting when an endpoint is returning a base object (ie.
Animal
) that can be specified more detailed by related type or when your endpoint returns a list of mixed objects.Retrofit example
The endpoint
GET /animals
might be returning a JSON similar toand ideally we should be able to decode it as
Without real support for polymorphism (as for version 1.3.0) the response would be decoded as
which leads to some information loss (the dog name was sent to the client and the client should have known about it)
Disclaimer: The above presented example is mostly to help understand the feature that polymorphism brings to codegen.
An other possibility might be to define inheritance via sealed classes
Does someone have other ideas?
The text was updated successfully, but these errors were encountered: