Questions tagged [sealed-class]
142 questions
7
votes
0 answers
How to encode Algebraic Data Types in current Dart?
How to encode Algebraic Data Types current in Dart?
Algebraic data types are also known as:
Sealed Classes in Kotlin
Algebraic Data Types in Scala
Enums in Rust
Enums with Associated Values in Swift
etc etc.
Question is, how do I implement it in…

VasiliNovikov
- 9,681
- 4
- 44
- 62
7
votes
1 answer
JDK 15 Sealed Classes - how to use across packages?
I have a simple sealed class, MyShape:
public sealed class MyShape permits MyCircle {
private final int width;
private final int height;
public MyShape(int width, int height) {
this.width = width;
this.height = height;
…

Luke
- 2,151
- 3
- 17
- 15
7
votes
2 answers
Android Room, how to save an entity with one of the variables being a sealed class object
I want to save in my Room database an object where one of the variables can either be of on type or another. I thought a sealed class would make sense, so I took this approach:
sealed class BluetoothMessageType() {
data class Dbm(
val…

Stylianos Gakis
- 862
- 8
- 19
7
votes
1 answer
How to get rid of this boilerplate code in this sealed class hierarchy?
Suppose I've got a sealed class hierarchy like that:
sealed class A {
abstract val x: Int
abstract fun copyX(x1: Int): A
}
data class A1(override val x: Int, val s1: String) : A() {
override fun copyX(x1: Int): A {
return…

Michael
- 41,026
- 70
- 193
- 341
6
votes
2 answers
Is the permits relationship of Java Sealed classes/interfaces transitive
If I read the JLS §8.1.6 and §9.1.4 correctly, the classes that a sealed class/interface permits, are just the direct subclasses/interfaces.
To illustrate this, consider the following example:
public sealed interface I1 permits I2, C, D { /*...*/…

twwwt
- 438
- 1
- 4
- 16
6
votes
1 answer
Sealed classes in Java 17, and the open-closed principle
For the first time in a LTS version of Java (Java 17) we have the sealed keyword that, in a nutshell, give us the possibility to restrict the hierarchy:
public abstract sealed class Person
permits Employee, Manager {
//...
}
If I want to…

SGiux
- 619
- 3
- 10
- 34
6
votes
4 answers
How to init a sealed-class in MutableStateFlow
I have a sealed-class like this
sealed class LoadState {
class Loading : LoadState()
class Success : LoadState()
class Fail : LoadState()
}
I use sealed-class with LiveData, it works
open class BaseViewModel: ViewModel() {
// val…

kokod21
- 61
- 1
- 2
6
votes
3 answers
Serialize Sealed class within a data class using Gson in kotlin
I have created a sealed class for the json field Value under CustomAttribute data class. This field can return String or Array of Strings.
How can we deserialize this sealed class from json?
data class CustomAttribute (
val attributeCode:…

Akash Bisariya
- 3,855
- 2
- 30
- 42
6
votes
1 answer
Benefits of declaring a sealed class as object
Context
I have this sealed class and each of its children:
sealed class Section
class SearchSection : Section()
class FavoritesSection : Section()
class RecommendationsSection : Section()
class ProfileSection : Section()
But I get a warning over…

Daniel Gomez Rico
- 15,026
- 20
- 92
- 162
5
votes
2 answers
Sealed class as Entity
I have an issue with sealed classes. If I run my application from docker it works perfectly fine, but if I do the same in IntelliJ I run into the following exception:
java.lang.IncompatibleClassChangeError: class…

Botond Németh
- 61
- 5
5
votes
1 answer
Difference between sealed and final class in Java
I have just read that Java 15 should have sealed classes.
jep360 says:
Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.
I thought that this is exactly what a final class does in Java.
So now I…

anion
- 1,516
- 1
- 21
- 35
5
votes
1 answer
When are sealed classes and records used together in Java?
JEP on sealed classes says :
Sealed classes do not depend on records (JEP 384) or pattern matching (JEP 375), but they work well with both.
What does it mean "work well"? Are there any recommendations to use the combination in some specific cases?

Serhii Povísenko
- 3,352
- 1
- 30
- 48
5
votes
0 answers
Sealed class for Repository to ViewModel comunication
Based on this post by Florina Muntenescu I have 2 questions:
Question 1: In the following situation considering a Respository response SomeApiResult:
sealed class SomeApiResult {
object Success : SomeApiResult()
object…

GuilhE
- 11,591
- 16
- 75
- 116
5
votes
3 answers
What is the Performance impact of Sealed Classes in the context of Android?
In Android, it is advisable to refrain from using enum due to performance issue. This is true until recently, where it was announced in Google IO 2018 that enums are now safe to use though avoiding them is still advisable for a more performant…

Archie G. Quiñones
- 11,638
- 18
- 65
- 107
5
votes
2 answers
Sealed class in Kotlin, Incompatible types error
I have the following Kotlin code. A sealed class called Animal, and two object classes Dog and Cat inherits from the sealed class Animal. I am getting this error in the when clause in the is Cat case.
Incompatible types: Cat and Dog
Why is it…

s-hunter
- 24,172
- 16
- 88
- 130