I have two types, one a subclass of the other, and...
# serialization
l
I have two types, one a subclass of the other, and I’m trying to make it work with the serialization library:
Copy code
@Serializable
open class Foo(open val foo: String)

@Serializable
data class Bar(override val foo: String, val bar: String) : Foo(foo)
The compiler is pointing on
Bar
that I have
foo
declared twice (
has duplicate serial name of property foo, either in it or its parents
). I could not find this scenario on the polymorphism doc. Does anyone know how to get around this?
f
You can do something like this at the cost of making
Foo
abstract:
Copy code
@Serializable
abstract class Foo {
    abstract val foo : String
}

@Serializable
data class Bar(override val foo: String, val bar: String) : Foo()
This is usually acceptable because non-abstract non-interface open classes are frowned upon these days. If you need
Foo
to not be abstract I dont know. Not being able to override concrete vals is probably not intended, and could be grounds for an issue.
l
Thanks Fudge, that’s a good option, but i need serialization to be able to instantiate
Foo
too. This is a sample of a problem, but the real class has about 12 properties that I’d have to repeat on `Bar`` 😕