Kotlin sealed class with generic covariance

Kotlin sealed class with generic covariance

Covariance as a contract with strict rules make class type predictable and safe. Covariance approach let us to narrow the type of certain method.

Covariance approach let us to narrow the type of certain method, by example: Int type is a part of super type Any.

Kotlin provides ‘In’, ‘out’ reserved keywords — to enhance our tools of using covariance/contravariance.

For ‘in’ generic, we could assign a class of super-type to class of sub-type. But for ‘out’ generic, we could assign a class of sub-type to class of super-type.

If our class used generic type as the output of the function, we use ‘out’ in covariance logic. We pass type as a parameter to the Interface to be strict and concise in terms of functionality we use. ‘Out’ indicates that T is intended to be produced by the methods of this class.

In fact,

1. Use in when super type could be assigned sub type.

2. Use out when sub type could be assigned to super type.

With courtesy by Kt.Academy by Marcin Moskala

With courtesy of Kt.Academy by Marcin Moskala

By example, in real world we need to show a set of possibilities: class with a methods about Success, Failure or Proceeding state.

Sure, first thing we can useenum. But enum have their limitations: each sub type can be only a constant and it has no state.

fun action(x: Int): String {
    val foo = when (x) {
        PaymentStatus.PAID.value -> "PAID"
        PaymentStatus.UNPAID.value -> "UNPAID"
        else -> throw IllegalStateException()
    }
    return foo
}
enum class PaymentStatus(val value: Int) {
    PAID(1),
    UNPAID(2)
}
fun main() {
    println(action(1))
}

kotlin when generics covariance sealed-classes

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Generics in Kotlin

Generics is a facility for generic programming, allowing classes, interfaces and methods to operate on various types of objects.

Kotlin sealed class for success and error handling

Handling errors has never been easy for Java programming language since the lack of supports from the Java compiler.

Learn Kotlin for Android: Sealed Classes

Learn Kotlin for Android: Sealed Classes. In this tutorial we will be talking about Sealed Classes. I forgot to mention that Sealed Classes cannot be inherited from.

Enum vs Sealed class — which one to choose?

Enums have supporting functions like valueOf, values or enumValues what makes them easier to iterate over or serialize.

Generics & Abstract Classes in TypeScript

In the above case the input and output are specified as type any which defeats the whole purpose of TypeScript as it will never find a type error if anything is allowed. Instead we could use generic syntax as denoted by <> to pass in a generic type when the function is called.