Questions tagged [sealed-class]
142 questions
1
vote
0 answers
Dart Sealed Class Compile Time Error: "The type 'PossibleErrors' is not exhaustively matched by the switch cases."
I am experimenting on dart`s new feature sealed classes and using it on a switch statement. I get the compile time error: The type 'PossibleErrors' is not exhaustively matched by the switch cases. Try adding a default case or cases that match…

D. Ndungu
- 131
- 1
- 8
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
2 answers
Kotlin sealed classes vs using polymorphism
I'm curious about an example given in Kotlin documentation regarding sealed classes:
fun log(e: Error) = when(e) {
is FileReadError -> { println("Error while reading file ${e.file}") }
is DatabaseError -> { println("Error while reading from…

Sadeq Dousti
- 3,346
- 6
- 35
- 53
1
vote
2 answers
How to create a sealed abstract class in TypeScript?
In Kotlin, a sealed class is an abstract class whose direct subclasses are known at compile time. All the direct subclasses of the sealed class must be defined in the same module as the sealed class. No class defined in any other module can extend…

Aadit M Shah
- 72,912
- 30
- 168
- 299
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
1
vote
1 answer
Accessing the nested level of sealed class in android kotlin
I have a sealed class
MyEvents.kt
sealed class MyEvents{
sealed class HelloBroadcasting : MyEvents() {
sealed class FeatureSegments : HelloBroadcasting() {
class…

Devrath
- 42,072
- 54
- 195
- 297
1
vote
1 answer
Kotlin sealed class subtyping in Jackson
I have following classes:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
JsonSubTypes.Type(value = Speech::class, name = "Speech"),
JsonSubTypes.Type(value = Audio::class, name…

pixel
- 24,905
- 36
- 149
- 251
1
vote
0 answers
Checking to see if two objects within the same sealed class are of the same type
I have a sealed class declared as follows:
sealed class SealedClass {
object Pizza: SealedClass()
data class Hamburger(val hasCheese: Boolean): SealedClass()
}
I have a map that maps SealedClass keys to String values and I would like to…

mooglin
- 500
- 5
- 17
1
vote
1 answer
How to use sealed classes in Android using Kotlin?
I am not able to understand how/when to use sealed classes in Android using Kotlin. I have read the docs but still I am getting confused about its' structure and how to use it. It'd of great help if someone could help me understand this.
user16658145
1
vote
1 answer
how to implement Result.Success and Result.failure correctly in ViewModel with coroutines?
I have viewmodel where I am getting response with following way
@HiltViewModel
class GiphyTaskViewModel
@Inject
constructor(private val giphyTaskRepository: GiphyTaskRepository):ViewModel()
{
var…
user16305099
1
vote
3 answers
How do I override sealed class fields?
I created a custom Result field (And not Kotlin's Result) in my service so I can return a message field in Both Success & Failure Cases:
sealed class Result {
data class Success(val value: T, val message: String) : Result()
data…

ran8080
- 41
- 1
- 3
1
vote
1 answer
What happens when a constructor of an abstract class is private in kotlin?
I don't understand what happens when a constructor of an abstract class is private in Kotlin. In this example I am using a sealed class and it has a private constructor by default. However, I am able to call the private constructor from…

Marti Serra Molina
- 423
- 3
- 11
1
vote
1 answer
How to create a class inheriting from a sealed and a public class at the same time
I have a sealed and public class objects consumed in many classes of the project.
public sealed class Customer {
public int Id{get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string MiddleName {get;…

ChinnaR
- 797
- 3
- 9
- 24
1
vote
2 answers
KOTLIN: Refactoring a big return when conditional clause coupled with another one
'Tis a design and technical code-wise question, so during some refactoring I was doing on a random project to bring it up to date, I encountered the following part, an infix function on a sealed class that was doing some sort of validation:
infix…

g0dzax
- 131
- 8
1
vote
0 answers
Retrofit & Moshi: Get request with sealed class & generics - Is it possible?
I have a sealed class for state handling of my Retrofit responses. It's members take a generic type. I would like to get Retrofit to be able to return the proper object, but I am stuck at this error: Unable to create converter for…

michpohl
- 852
- 8
- 30