Kotlin sealed classes assigning property constants - kotlin

kotlin_version = '1.2.30'
I have a sqlite table that has a Integer value for a column called direction. That will store the Integer property based on the enum constant.
i.e will insert 40 into the table:
saveDirection(Direction.Right.code)
I have a enum class written in kotlin with property constants assigned.
enum class Direction(val code: Int) {
UP(10),
DOWN(20),
LEFT(30),
RIGHT(40),
NONE(0)
}
I am wondering if I can do the same with sealed classes
sealed class Direction {
abstract val code: Int
data class Up(override val code: Int): Direction()
data class Down(override val code: Int): Direction()
data class Left(override val code: Int): Direction()
data class Right(override val code: Int): Direction()
data class None(override val code: Int): Direction()
}
However, this won't work as the saveDirection(direction: Int) is expecting an Int value:
saveDirection(Direction.Right(40))
Is this possible to assign constant properties to sealed classes so that you can get the constant property like in enums?
Thanks for any suggestions,

You could use sealed classes like this:
sealed class Direction(val code: Int) {
override fun equals(other: Any?): Boolean = other is Direction && code == other.code
override fun hashCode(): Int = code
}
class Up : Direction(10)
class Down : Direction(20)
class Left : Direction(30)
class Right : Direction(40)
class None : Direction(0)
However, given the limited context of the question, it is unclear what exactly you would gain by this. In fact, in this simple case, Kotlin does not allow your subclasses to be marked as data class:
Data class must have at least one primary constructor parameter
The solution provided above does not have any advantage over enums, in fact it is more verbose and error-prone than an enum-based definition, so why not just use those?

Related

Serialization with sealed classes fails in Kotlin serialization

I'm having trouble with kotlin-serialization in the following use case:
#Serializable
sealed class NetworkAnswer {
#SerialName("answerId")
abstract val id: Int
}
#Serializable
data class NetworkYesNoAnswer(
override val id: Int,
#SerialName("isPositive")
val isPositive: Boolean
) : NetworkAnswer()
When I serialize this:
val json = Json { ignoreUnknownKeys = true; explicitNulls = false }
val result: NetworkYesNoAnswer = json.decodeFromString(NetworkYesNoAnswer.serializer(), """
{
"answerId": 1,
"isPositive": true
}
""".trimIndent()
)
I get the following error
Caused by: kotlinx.serialization.MissingFieldException: Fields [id] are required for type with serial name 'NetworkYesNoAnswer', but they were missing
The only way the serialization works is if I use the same name for both the member and "SerialName", like so:
#Serializable
sealed class NetworkAnswer {
#SerialName("answerId")
abstract val answerId: Int
}
#Serializable
data class NetworkYesNoAnswer(
override val answerId: Int,
#SerialName("isPositive")
val isPositive: Boolean
) : NetworkAnswer()
This kinda defeats the purpose of "SerialName", is there a way to solve that without using the same name?
Declaring a #SerialName on a base class has no effect on member declarations overridden by child classes.
Instead, you can declare #SerialName on the child class instead. There is no need to change the actual name of the field.
#Serializable
data class NetworkYesNoAnswer(
#SerialName("answerId")
override val id: Int,
#SerialName("isPositive")
val isPositive: Boolean
) : NetworkAnswer()
Declaring the #SerialName on the base class and applying it to all children seems NOT to be supported as of now, but is desired by other members of the community as well, e.g. here on GitHub.
OT: Most likely you could use a sealed interface, which was first introduced in Kotlin v1.5.0, instead of a sealed class.

Override getters in Kotlin?

So I have an abstract class Composition, which has two children: one is a Track, and one is an Album (which is a group of Tracks).
class Composition(val name: String, ...)
class Track(name: String): Composition(name)
class Album(name: String, val tracks: List<Track>): Composition(name)
So far, so good. Now, I have the duration that is added. It is abstract in Composition, so I can override it in the children:
abstract class Composition(...){
abstract fun getDuration(): Int
}
Now, I can add override the method in the Track, which takes it as a parameter:
class Track(..., private val duration: Int): Composition(...){
override fun getDuration() = duration
}
And finally, I make the Album, whose duration is the sum of the Tracks:
class Album(..., val tracks: List<Track>): Composition(...){
override fun getDuration() = tracks.sumBy { it.getDuration() }
}
It works as intended, but I do not understand why I cannot simply use tracks.sumBy { it.duration }, since in Kotlin properties are nothing more than getters and setters (I'm thinking about the getDuration in Composition).
I feel like I'm missing something, because if the same code was written in Java, I would be able to call composition.duration as a property -- so that makes me think that Kotlin allows it from Java code, but not from Kotlin code, which is sad.
An other example:
Let's say I have a class named Artist, who wrote multiple Compositions:
class Artist(
val nom: String,
private val _compositions: MutableList<Composition> = ArrayList()
) {
// HERE (I wrote the extension method List<E>.toImmutableList)
fun getCompositions() : List<Composition> = _compositions.toImmutableList()
}
This is standard in Java (exposing immutable versions of Collections via getters, so they are not modified) ; Kotlin doesn't recognize it though:
val artist = Artist("Mozart")
artist.getCompositions() // Legal
artist.compositions // Illegal
I thought about making this a property, but:
- If I choose the type List<E>, I can override the getter to return the immutable list, but I cannot use regular methods (add...) as the List is immutable
- If I choose the type MutableList<E>, I cannot override the getter to return ImmutableList (which is a subclass of List that I wrote, and is obviously not a subclass of MutableList).
There's a chance I'm doing something ridiculous while there is an easy solution, but right now I cannot find it.
In the end, my question is: Why aren't manually-written getters considered properties when written from Kotlin?
And, if I'm mistaking, What is the expected way of solving both of these patterns?
If you want to use it as property, you should use Kotlin-way to override getter.
For example:
abstract class Composition(...){
abstract val duration: Int
}
// You can use "override" in constructor
// val - is immutable property that has only getter so you can just
// remove private modifier to make possible get it.
class Track(..., override val duration: Int): Composition(...){
...
}
class Album(..., val tracks: List<Track>): Composition(...) {
override val duration: Int
get() = tracks.sumBy { it.duration }
}
Also there are may be case when you need mutable property that can be changed only inside of object. For this case you can declare mutable property with private setter:
class SomeClass(value: Int) {
var value: Int = value
private set
}
Read more in docs: https://kotlinlang.org/docs/reference/properties.html#getters-and-setters
You have to define duration as an abstract property and not as an abtract function (https://kotlinlang.org/docs/reference/properties.html#getters-and-setters):
abstract class Composition(val name: String) {
abstract val duration: Int
}
class Track(name: String, override val duration: Int): Composition(name)
class Album(name: String, val tracks: List<Track>): Composition(name) {
override val duration: Int
get() = tracks.sumBy { it.duration }
}
The getter/setter conversion as properties does only work for Java classes (https://kotlinlang.org/docs/reference/java-interop.html#getters-and-setters).

The best way to override a property as a constant in Kotlin

Kotlin provides support for property overriding. I an wondering what is the best way to override a property as a constant value.
To be more specific, assume that an abstract val property is declared in a superclass or an interface:
// In a superclass or an interface
abstract val num : Int
In its subclass there are at least 2 ways as far as I can think of to override it:
// In its subclass
override val num : Int = 0
or
// In its subclass
override val num : Int get() = 0
Besides the two, I can also do it in the Java way:
// In a superclass or an interface
abstract fun getNum() : Int
// In its subclass
override fun getNum() : Int = 0
What's the difference among the three in terms of memory and generated bytecode? Which one is the best?
Are there even better ways or patterns in Kotlin to declare properties that are to be overridden as constants?
There's functional difference.
Using assignment You initialize the field when object is created:
override val num : Int = 0
This creates an implicit final backing field with value 0, and getter that always returns same value. This is generated bytecode decompiled into java:
private final int num;
public int getNum() {
return this.num;
}
Second declaration is actually a getter override, which is also a valid way to implement property from the interface. This does not create a backing field, so your property can return different values on each call (for example method call):
override val num : Int get() = 0
Decompiled bytecode:
public int getNum() {
return 0;
}

Kotlin: Issue implementing interface with getters

I am trying to implement an interface with getter method which matches the constructor parameter name of the implementing class.
interface Car{
fun getModel(): Int
}
class Honda(val model: Int): Car {
override fun getModel(): Int {
}
}
If Honda doesn't implement getModel(), we get an Accidental Override error. If Honda implements getModel(), we get a Platform declaration clash error.
I can change the name of the parameter in the Honda constructor, which fixes the problem, but it feels like a redundant getter method.
interface Car{
fun getModel(): Int
}
class Honda(val modelParam: Int): Car {
override fun getModel() = modelParam
}
Is there a better way to implement such interfaces?
You can declare properties in interface:
interface Car{
val model : Int
}
Then in implementation / constructor you need to add override keyword.
class Honda(override val model : Int): Car
For case where the accepted answer isn't applicable because you can't change the interface, or the interface is a Java one,
class Honda(private val model: Int): Car {
override fun getModel(): Int = model
}
For a Java interface, it can still be accessed as .model in Kotlin.

Searching a workaround for kotlin empty data class primary constructor

With given kotlin code :
sealed class Event(val id:String= UUID.randomUUID().toString(), val timestamp:Instant = Instant.now())
data class BarEvent(val additionalInfo:String):Event()
object FooEvent:Event()
// data class CorrectFooEvent():Event() // invalid kotlin
fun main(args: Array<String>) {
val b1 = BarEvent("b1")
val f1 = FooEvent
Thread.sleep(1000)
val b2 = BarEvent("b2")
val f2 = FooEvent
println("${b1.id} ${b1.timestamp} $b1")
println("${f1.id} ${f1.timestamp} $f1")
println("${b2.id} ${b2.timestamp} $b2")
println("${f2.id} ${f2.timestamp} $f2")
}
There is no issue with BarEvent.
But as FooEvent has no more parameter than the ones in Event, I would like it to have empty constructor. It's not authorized for data class, so I made it an object. But object is singleton, so it doesn't behave as an instanciated event.
The only workaround that I see (keeping the class as a data class) is something like :
sealed class Event(open val id:String= UUID.randomUUID().toString(), open val timestamp:Instant = Instant.now())
data class FooEvent(override val id:String= UUID.randomUUID().toString(), override val timestamp:Instant = Instant.now()):Event()
But it's not very elegant.
Just change FooEvent to a normal class, and add (or generate them using your IDE) toString(), hashCode() and equals(Object) if needed:
class FooEvent: Event() {
override hashCode() = ...
override equals(other: Object) {
...
}
override toString() = ...
}
To make the event a data class, simply add an unused property to it. Not pretty, but as short as it can be in Kotlin at the moment:
data class FooEvent(val dummy: Unit = Unit) : Event()
There seems to be no intention to remove this limitation soon:
Data class without arguments deprecated in 1.0. Why?
Suggestion for parameterless data class