Questions tagged [sealed-class]
142 questions
4
votes
1 answer
With the new Dart sealed-classes feature, is it possible to do a nested switch?
I have a sealed class for FailureOrSuccess, with subclasses for Success, and Failure.ch
The Failure subclass is also a sealed class, with multiple subclasses for the different types of failure.
I would like to have a switch statement that switches…

Charlie Page
- 541
- 2
- 17
4
votes
2 answers
What is the difference between Sealed class and inheritance principle in Kotlin?
I'm new with Kotlin. I'm reading a book and a sealed class is displayed there as an "extension" of Enum. I can't see the similarity between them. The way I see the things, Sealed class is more related to inheritance, because each class can inherit…

Eitanos30
- 1,331
- 11
- 19
4
votes
1 answer
Initializing companion object after inner objects
Let's say I want to create sealed class, filled with some objects. Then I want to create list of all such objects, so I create list in companion object:
fun main() {
println(Color.Blue)
println(Color.allColors)
}
sealed class Color {
…

Matej Drobnič
- 981
- 10
- 18
3
votes
3 answers
Make class sealed by default
I have many code styles specified in my .editorconfig file for my C# projects. I would find really useful if all of my classes could be sealed by default (When you create a new one) or at least they would show warning there class is not sealed. Is…

Kebechet
- 1,461
- 15
- 31
3
votes
1 answer
Android - How to make sealed class extend other sealed class?
I have a sealed interface for my UIState.
sealed interface UIState {
object ShowLoading : UIState
object ShowEmptyData : UIState
data class ShowData(val data: T) : UIState
data class ShowError(val…

Hayk Mkrtchyan
- 2,835
- 3
- 19
- 61
3
votes
2 answers
When in Kotlin Either Hell
I am trying to use
Arrow Either results instead of try-catch, but have gone too deep down the rabbit hole.
I have been trying to use Either as my functional return types, where Problem is like
sealed interface Problem
data class…

Eric Kolotyluk
- 1,958
- 2
- 21
- 30
3
votes
3 answers
In C#, is it possible to mock out IMessageReceiver and related classes for unit testing?
I have the following class I wish to create unit tests for:
public class ServiceBusClient {
private readonly IMessageReceiver messageReceiver;
private readonly int maximumMessages;
public…

Brian Kessler
- 2,187
- 6
- 28
- 58
3
votes
1 answer
How can I use sealed classes to describe a finite set of cases with associated values, and a smaller set of such values?
I am looking at using sealed class to represent a finite set of possible values.
This is part of a codegeneration project that will write a very large number of such classes, which can each have a lot of cases. I am therefore concerned about app…

Guig
- 9,891
- 7
- 64
- 126
3
votes
1 answer
Force compiler to emit an error when not all implementations are covered in "when" statement
Maybe this is an absurd question. I have a method that receives a Command (sealed class) and returns Unit and I want the compiler to crash whether all the when branches have not been implemented:
sealed class Command
class FirstCommand :…

Héctor
- 24,444
- 35
- 132
- 243
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…

rishi kumar
- 61
- 5
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
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
2
votes
1 answer
Kotlin - categorized subenums
I am trying to implement a Role class/interface/enum that I can use throughout my program. I want the roles to be somewhat categorized - I want some roles to be of type A, some roles to be of type B, and some roles to be part of multiple types. I…

Guy Schwartzberg
- 23
- 3
2
votes
2 answers
Kotlin sealed classes and hashcode/equals
I'm seeing writing a test that I cannot assert two sealed classes with same "subclass" and same value under the hood are equal. They are distinct.
fun main() {
val a1 = MySealed.A("foo")
val a2 = MySealed.A("foo")
System.out.println(a1…

cort
- 1,088
- 1
- 11
- 20
2
votes
2 answers
Kotlin Flow - Some emitted events not received when collect
I am using MutableStateFlow. My flow type is sealed class with different states (Loading, Success, Error, etc). Initial value of my flow is empty:
private val _updateDepartmentsState =…

testivanivan
- 967
- 13
- 36