Questions tagged [kotlin-sealed]

17 questions
53
votes
2 answers

What is the difference between sealed class vs sealed interface in kotlin

With Kotlin 1.5 was introduce the sealed interface. Even that I know the difference between classes and interfaces, I'm not clear what are the best practices and beneficies of using sealed interface over sealed class Should I always use interface…
Canato
  • 3,598
  • 5
  • 33
  • 57
9
votes
1 answer

How to fix "Non exhaustive 'when' statements on sealed class/interface" in Kotlin

Non exhaustive when statements on sealed class/interface will be prohibited in Kotlin 1.7. I have a sealed class State and it's children: sealed class State { object Initializing : State() object Connecting : State() object Disconnecting…
Sergio
  • 27,326
  • 8
  • 128
  • 149
2
votes
1 answer

[Android][Room] Storing Sealed classes into Room Databse

There is no default support for inserting or retreiving kotlin sealed classes into the ROOM database. Enums could be easily work, but not SEALED classes. Below is my example for a DOWNLOADSTATUS seal @Entity(tableName = "SomeTableName") data class…
2
votes
2 answers

Kotlin - make multiple sealed classes have common set of "base" subclasses, but each could still add it's specific ones

This question has a wider scope than Extract common objects from sealed class in kotlin and Android - How to make sealed class extend other sealed class? so it's not a duplicate of these I have multiple sealed classes that represent results of…
Piotr Śmietana
  • 393
  • 2
  • 19
2
votes
1 answer

Constraint on function type using data kind

I'm confused about data kinds. Suppose we have {-# LANGUAGE DataKinds #-} ... data Format = Photo { bytes :: Int } | Video { bytes :: Int , durationSec :: Int } I want to make then function with promouted…
2
votes
1 answer

How to use sealed classes with generics in kotlin`

I have the following classes but I'm struggling with generics sealed class Result { data class Success(val data: T): Result() data class Failure(val error: E): Result() } fun interface SeqListener { fun…
Matt Wolfe
  • 8,924
  • 8
  • 60
  • 77
1
vote
1 answer

Polymorphic serialization of sealed hierarchies with generic type parameters

Using Kotlin serialization, I would like to serialize and deserialize (to JSON) a generic data class with type parameter from a sealed hierarchy. However, I get a runtime exception. To reproduce the issue: import kotlinx.serialization.* import…
Ulrich Schuster
  • 1,670
  • 15
  • 24
1
vote
1 answer

Can I add an existing java class to a sealed Kotlin class?

I am using 2 classes to handle error statuses, the Spring's own org.springframework.http.HttpStatus and my custom ErrorStatus: enum class ErrorStatus(val code: Int, val reasonPhrase: String) { ELEMENT_NOT_FOUND(1404, "Element not found"), …
hc0re
  • 1,806
  • 2
  • 26
  • 61
1
vote
3 answers

Kotlin. How to get specific subclass of sealed class?

I'm using kotlin sealed class. And I need to retrieve specific subclass. My sealed class: sealed class Course( val type: Type ) { data class ProgrammingCourse(val name: String, val detail: String) : Course(Type.PROGRAMMING) object…
testivanivan
  • 967
  • 13
  • 36
0
votes
1 answer

com.fasterxml.jackson.databind.exc.MismatchedInputException: Kotlin sealed class Json Mapping

Using Spring Boot with Kotlin, Deserialization of this sealed class works for Range type but fails for Exactly type. The serialization works fine but the deserialization doesn't work. @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include =…
Ayoub
  • 361
  • 4
  • 15
0
votes
0 answers

Kotlin nested sealed classes for mapping enums

I have an extensive class hierarchy that I want to map to an existing enum type. Given this example hierarchy: open class A { open class BB: A() { class CCC: BB(){} class DDD: BB(){} } } I want to map the class type from the…
0
votes
1 answer

How to use sealed classes with generics in kotlin

I can't find a solution to this problem data class Data(val s: String) sealed class Base(val t: T, val f: Base.() -> Unit) class A(data: Data, f: A.() -> Unit) : Base(data, f) Type mismatch.Required:Base.() → Unit Found: A.() →…
0
votes
1 answer

Sealed data classes not considering parent property in equals

Consider the following: sealed class MySealedClass(val guid: String = UUID.randomUUID().toString()) { data class MyDataClass(var title: String) : MySealedClass() data class MyDataClass2(var something: String) : MySealedClass() } val…
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
0
votes
1 answer

Kotlin Sealed Classes

I am new to Kotlin Programming What are the real time examples of Sealed Classes. If the implemented classes are not sealed then it can be further inherited. What's the use in it?. Is Sealed class same as default modifier in java. (Except the when…
0
votes
1 answer

Kotlin serialization of value class that implements a sealed interface

I am trying to use Kotlin serialization (Kotlin 1.7.2, kotlinx.serialization 1.4.1) for value classes that implement a sealed interface: @Serializable sealed interface Power { val value: Int } @Serializable @JvmInline value class…
Ulrich Schuster
  • 1,670
  • 15
  • 24
1
2