# sealed class

Published articles for sealed class.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## UI State, Callbacks and Equality Pitfalls

DevFeed: [UI State, Callbacks and Equality Pitfalls](<https://devfeed.tech/articles/ui-state-callbacks-and-equality-pitfalls-25875.md>)

Original publisher: [Read original article](<http://lordraydenmk.github.io//2024/ui-state-callbacks/>)

Author: Stojan Anastasov

Published: 2024-11-30T00:00:00Z

Content type: tutorial

Language: en

Sources: [Stojan Anastasov's blog](<https://devfeed.tech/sources/stojan-anastasov-s-blog.md>)

Topics: [ui](<https://devfeed.tech/topics/ui.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Code](<https://devfeed.tech/topics/code.md>), [data](<https://devfeed.tech/topics/data.md>), [Compose](<https://devfeed.tech/topics/compose.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [code](<https://devfeed.tech/tags/code.md>), [data-class](<https://devfeed.tech/tags/data-class.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [pitfalls](<https://devfeed.tech/tags/pitfalls.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>), [state](<https://devfeed.tech/tags/state.md>), [ui](<https://devfeed.tech/tags/ui.md>)

### AI overview

This Kotlin article examines how callback functions embedded in UI state data classes can produce surprising equality results because function equality depends on reference identity. It explains the resulting update and performance issues in unidirectional data flow and discusses alternatives that keep action logic in the ViewModel or Presenter.

### Source excerpt

Yesterday a friend asked me to review his blog post on Function Properties in Data Classes are Code Smells. We then discussed how we would solve the issue in the context of UI state and callbacks. And I thought it might be useful to write it in form of a blog post. An example To better explain the problem, I'll start with a sample app I'm maintaining. It shows a list of superheroes from the Marvel API. See the screenshot below. Currently the UI state is modeled like: data class SuperheroViewEntity( val id: Long, val name: String, val imageUrl: HttpUrl ) Content( val superheroes: List<SuperheroViewEntity>, val copyright: String, ) // some states omitted for brevity A Naive Solution Let's say we have a new requirement, we want to add a favorite button to each superhero so we can keep track of our favorite superheroes. To do that we will introduce some functions: // ViewModel/Presenter fun onAddToFavorites(superheroId: Long) = TODO() fun onRemoveFromFavorites(superheroId: Long) = TODO() These functions can be part of the ViewModel/Presenter that delegates to a repository to store the IDs. Note: In this example, the ViewModel/Presenter use a single function per action the view can take. The technique described here also works with a single function + a sealed class for individual actions. To achieve this we might be tempted to update our SuperheroViewEntity to include: data class SuperheroViewEntity( val id: Long, val name: String, val imageUrl: HttpUrl, val onFavoriteClicked: () -> Unit ) // ViewModel fun Superhero.toViewEntity() = SuperheroViewEntity( id, name, imageUrl, if (favorite) { onRemoveFromFavorites(id) } else { onAddToFavorites(id) } ) // View layer Modifier.clickable { entity.onFavoriteClicked() } However this approach has some significant drawbacks. Functions in Kotlin are equal only if they are the same reference. val fa = { println(1) } val fb = { println(1) } println(fa == fb) // false, does the same job, but different reference val fc = fa println(fc =

## Hidden Gems of the Kotlin Standard Library - List

DevFeed: [Hidden Gems of the Kotlin Standard Library - List](<https://devfeed.tech/articles/hidden-gems-of-the-kotlin-standard-library-list-25100.md>)

Original publisher: [Read original article](<https://dladukedev.com/articles/045_hidden_gems_kotlin_standard_lib/>)

Published: 2024-10-03T00:00:00Z

Content type: tutorial

Language: en

Sources: [Donovan LaDuke - Developer](<https://devfeed.tech/sources/donovan-laduke-developer.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Library](<https://devfeed.tech/topics/library.md>)

Tags: [function](<https://devfeed.tech/tags/function.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-standard-library](<https://devfeed.tech/tags/kotlin-standard-library.md>), [library](<https://devfeed.tech/tags/library.md>), [list](<https://devfeed.tech/tags/list.md>), [overview](<https://devfeed.tech/tags/overview.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>), [standard](<https://devfeed.tech/tags/standard.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>)

### AI overview

An overview of useful Kotlin Standard Library functions for working with lists, including orEmpty, filterIsInstance, distinctBy, chunked, and random.

### Source excerpt

An overview of several hidden gem List Functions in the Kotlin Standard Library

## Sealed Types

DevFeed: [Sealed Types](<https://devfeed.tech/articles/sealed-types-25064.md>)

Original publisher: [Read original article](<https://typealias.com/start/kotlin-sealed-types/>)

Author: author@typealias.com (Dave Leeds)

Published: 2024-01-16T00:00:00Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [interfaces](<https://devfeed.tech/topics/interfaces.md>)

Tags: [classes](<https://devfeed.tech/tags/classes.md>), [enum](<https://devfeed.tech/tags/enum.md>), [exhaustive](<https://devfeed.tech/tags/exhaustive.md>), [exhaustive-matching](<https://devfeed.tech/tags/exhaustive-matching.md>), [interfaces](<https://devfeed.tech/tags/interfaces.md>), [introduction](<https://devfeed.tech/tags/introduction.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [learn-to-program](<https://devfeed.tech/tags/learn-to-program.md>), [programming](<https://devfeed.tech/tags/programming.md>), [sealed](<https://devfeed.tech/tags/sealed.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>), [sealed-interface](<https://devfeed.tech/tags/sealed-interface.md>), [subtype](<https://devfeed.tech/tags/subtype.md>), [types](<https://devfeed.tech/tags/types.md>), [when-conditional](<https://devfeed.tech/tags/when-conditional.md>), [when-expression](<https://devfeed.tech/tags/when-expression.md>)

### AI overview

A tutorial chapter explains how Kotlin sealed interfaces and classes restrict the set of possible types, helping ensure that all possibilities are handled in a when expression.

### Source excerpt

In Chapter 5, we saw how limiting our options can be a good thing. In that chapter, we used enum classes to limit our values, which allows Kotlin to ensure that we account for all possibilities in a when expression. We can get a similar benefit for our types by using sealed interfaces and classes. In this chapter, we'll visit Cecil's Ice Shop to learn all about sealed types. Let's get started!

## Covariant Nothing Object

DevFeed: [Covariant Nothing Object](<https://devfeed.tech/articles/covariant-nothing-object-39199.md>)

Original publisher: [Read original article](<https://kt.academy/article/ak-covariant-nothing-object>)

Published: 2023-02-13T00:15:00Z

Content type: tutorial

Language: en

Sources: [Kt. Academy](<https://devfeed.tech/sources/kt-academy.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [sealed class](<https://devfeed.tech/topics/sealed-class.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>), [Library](<https://devfeed.tech/topics/library.md>)

Tags: [data-structure](<https://devfeed.tech/tags/data-structure.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [object](<https://devfeed.tech/tags/object.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>), [type-parameter](<https://devfeed.tech/tags/type-parameter.md>), [workshop-learning-programming](<https://devfeed.tech/tags/workshop-learning-programming.md>)

### AI overview

This Kotlin developer article describes the "Covariant Nothing Object" pattern: using a covariant generic type with Nothing so a singleton object can represent an empty value across compatible types. It relates the pattern to immutable linked lists, Kotlin's standard library, generic messages, and scheduled tasks.

### Source excerpt

The pattern we are all using, but no-one talks about it.

## Made in Compose - Dynamic Island

DevFeed: [Made in Compose - Dynamic Island](<https://devfeed.tech/articles/made-in-compose-dynamic-island-25777.md>)

Original publisher: [Read original article](<https://www.sinasamaki.com/dynamic-island/>)

Author: sinasamaki

Published: 2023-01-10T07:33:57Z

Content type: tutorial

Language: en

Sources: [sinasamaki](<https://devfeed.tech/sources/sinasamaki.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [procedural flowers](<https://devfeed.tech/topics/procedural-flowers.md>)

Tags: [animation](<https://devfeed.tech/tags/animation.md>), [apple](<https://devfeed.tech/tags/apple.md>), [article](<https://devfeed.tech/tags/article.md>), [compose](<https://devfeed.tech/tags/compose.md>), [iphone](<https://devfeed.tech/tags/iphone.md>), [jetpack](<https://devfeed.tech/tags/jetpack.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>), [state](<https://devfeed.tech/tags/state.md>), [ui](<https://devfeed.tech/tags/ui.md>)

### AI overview

This tutorial explains how to recreate four Dynamic Island states from the iPhone 14 Pro in Jetpack Compose, including the default, face unlock, call, and call-and-timer states. It covers sealed classes for state management and animated content and size transitions.

### Source excerpt

Recreating the dynamic island animation from the new iPhone 14 Pro in Jetpack Compose.

## Bottom Navigation Animation in Jetpack Compose

DevFeed: [Bottom Navigation Animation in Jetpack Compose](<https://devfeed.tech/articles/bottom-navigation-animation-in-jetpack-compose-25767.md>)

Original publisher: [Read original article](<https://www.sinasamaki.com/bottom-navigation-animation-in-jetpack-compose/>)

Author: sinasamaki

Published: 2022-02-21T00:00:00Z

Content type: tutorial

Language: en

Sources: [sinasamaki](<https://devfeed.tech/sources/sinasamaki.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [navigation](<https://devfeed.tech/topics/navigation.md>), [Compose](<https://devfeed.tech/topics/compose.md>), [ui](<https://devfeed.tech/topics/ui.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [article](<https://devfeed.tech/tags/article.md>), [bottom-navigation](<https://devfeed.tech/tags/bottom-navigation.md>), [class](<https://devfeed.tech/tags/class.md>), [code](<https://devfeed.tech/tags/code.md>), [color](<https://devfeed.tech/tags/color.md>), [components](<https://devfeed.tech/tags/components.md>), [compose](<https://devfeed.tech/tags/compose.md>), [icons](<https://devfeed.tech/tags/icons.md>), [implement](<https://devfeed.tech/tags/implement.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [navigation](<https://devfeed.tech/tags/navigation.md>), [screen](<https://devfeed.tech/tags/screen.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>), [state](<https://devfeed.tech/tags/state.md>), [ui](<https://devfeed.tech/tags/ui.md>)

### AI overview

A tutorial on building a custom animated bottom navigation interface in Jetpack Compose. It starts with a functional navigation UI and then adds animations for item weight, size, height, shadows, corners, opacity, and title visibility using Compose animation components.

### Source excerpt

Bottom navigation UI is used to navigate between different top level screens in apps. There is a composable available in the compose libraries, but it could be improved with some more animations. Using only the animation components provided in the jetpack compose libraries, we could implement a more custom bottom

## Advent of Kotlin Solutions

DevFeed: [Advent of Kotlin Solutions](<https://devfeed.tech/articles/advent-of-kotlin-solutions-39189.md>)

Original publisher: [Read original article](<https://kt.academy/article/advent-2021-solutions>)

Published: 2022-01-02T00:00:00Z

Content type: tutorial

Language: en

Sources: [Kt. Academy](<https://devfeed.tech/sources/kt-academy.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [clustering](<https://devfeed.tech/topics/clustering.md>), [JSON](<https://devfeed.tech/topics/json.md>)

Tags: [algorithms](<https://devfeed.tech/tags/algorithms.md>), [clustering](<https://devfeed.tech/tags/clustering.md>), [json](<https://devfeed.tech/tags/json.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [pattern-matching](<https://devfeed.tech/tags/pattern-matching.md>), [recursion](<https://devfeed.tech/tags/recursion.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>), [workshop-learning-programming](<https://devfeed.tech/tags/workshop-learning-programming.md>)

### AI overview

This article presents Kotlin solutions for Advent of Kotlin 2021 exercises, including JSON stringification, generating well-formed parentheses, tree algorithms, and k-means clustering. It discusses recursion, sealed classes, smart casting, and Kotlin functions used in the solutions.

### Source excerpt

Solutions to the Advent of Kotlin!

## Sealed interfaces in Kotlin

DevFeed: [Sealed interfaces in Kotlin](<https://devfeed.tech/articles/sealed-interfaces-in-kotlin-27470.md>)

Original publisher: [Read original article](<https://jorgecastilloprz.github.io/sealed-interfaces-kotlin>)

Author: Jorge Castillo

Published: 2021-03-06T10:00:00Z

Content type: tutorial

Language: en

Sources: [👨💻 Jorge Castillo](<https://devfeed.tech/sources/jorge-castillo.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [interfaces](<https://devfeed.tech/topics/interfaces.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Library](<https://devfeed.tech/topics/library.md>), [enum](<https://devfeed.tech/topics/enum.md>)

Tags: [enum](<https://devfeed.tech/tags/enum.md>), [interface](<https://devfeed.tech/tags/interface.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-compiler](<https://devfeed.tech/tags/kotlin-compiler.md>), [sealed](<https://devfeed.tech/tags/sealed.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>)

### AI overview

An overview of experimental sealed interfaces planned for Kotlin 1.5. The article explains relaxed subclass-location restrictions, allowing implementations in different files within the same module, and discusses why sealed interfaces can cover cases that sealed classes cannot, including enums implementing interfaces and types belonging to multiple sealed hierarchies.

### Source excerpt

Short overview of the sealed interfaces coming up in Kotlin 1.5.

## Modelling UI State on Android

DevFeed: [Modelling UI State on Android](<https://devfeed.tech/articles/modelling-ui-state-on-android-25873.md>)

Original publisher: [Read original article](<http://lordraydenmk.github.io//2021/modelling-ui-state/>)

Author: Stojan Anastasov

Published: 2021-01-29T00:00:00Z

Content type: tutorial

Language: en

Sources: [Stojan Anastasov's blog](<https://devfeed.tech/sources/stojan-anastasov-s-blog.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [android-development](<https://devfeed.tech/topics/android-development.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>), [Development](<https://devfeed.tech/topics/development.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-development](<https://devfeed.tech/tags/android-development.md>), [cardinality](<https://devfeed.tech/tags/cardinality.md>), [data-class](<https://devfeed.tech/tags/data-class.md>), [data-structure](<https://devfeed.tech/tags/data-structure.md>), [fp](<https://devfeed.tech/tags/fp.md>), [functional](<https://devfeed.tech/tags/functional.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [livedata](<https://devfeed.tech/tags/livedata.md>), [rxjava](<https://devfeed.tech/tags/rxjava.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>), [stateflow](<https://devfeed.tech/tags/stateflow.md>), [types](<https://devfeed.tech/tags/types.md>), [ui](<https://devfeed.tech/tags/ui.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

### AI overview

This article explains how to model Android UI state in Kotlin using data classes and sealed classes. It connects types with sets and cardinality, then describes product types and sum types as tools for representing valid application states.

### Source excerpt

The recommended approach from Google for Android development is holding the UI state in a ViewModel and having the View observe it. To achieve that one can use LiveData, StateFlow, RxJava or a similar tool. But how to model the UI state? Use a data class or a sealed class? Use one observable property or many? I will describe the tradeoffs between the approaches and present a tool to help you decide which one to use. This article is heavily inspired by Types as Sets from the Elm guide, a large part is a translation from Elm to Kotlin. Photo by Marc-Olivier Jodoin on Unsplash Types as sets By Making Data Structure we can make sure the possible values in code exactly match the valid values in real life. Doing that helps to avoid a whole class of bugs related to invalid data. To achieve that, first we need to understand the relationship between Types and Sets. We can think of Types as sets of values, they contain unique elements and there is no ordering between them. For example: Nothing - the empty set, it contains no elements Unit - the singleton set, it contains one element - Unit Boolean - contains the elements true and false Int - contains the elements: ... -2, -1, 0, 1, 2 ... Float - contains the elements: 0.1, 0.01, 1.0 .... String - contains the elements: "", "a", "b", "Kotlin", "Android", "Hello world!"... So when you write: val x: Boolean it means x belongs to the set of Boolean values and can be either true or false. Cardinality In Mathematics, Cardinality is the measure of "number of elements" of a Set. For example the set of Boolean contains the elements [true, false] so it has a cardinality = 2. Let's take a look at the cardinality of the sets mentioned above: Nothing - 0 Unit - 1 Boolean - 2 Short - 65535 Int - ∞ Float - ∞ String- ∞ Note: The cardinality of Int and Float is not exactly infinity, it's 2^32 however that is a huge number. When building apps, we use built-in types and create custom types using constructs like data classes and sealed classes. Product

## Kotlin string templates, destructuring declarations, and sealed classes

DevFeed: [Kotlin string templates, destructuring declarations, and sealed classes](<https://devfeed.tech/articles/cooking-tasty-code-in-kotlin-part-2-25710.md>)

Original publisher: [Read original article](<https://blog.shreyaspatil.dev/cooking-tasty-code-in-kotlin-part-2/>)

Author: Shreyas Patil

Published: 2021-01-01T09:23:42Z

Content type: tutorial

Language: en

Sources: [Shreyas Patil's Blog](<https://devfeed.tech/sources/shreyas-patil-s-blog.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [coding](<https://devfeed.tech/topics/coding.md>), [coding style](<https://devfeed.tech/topics/coding-style.md>)

Tags: [coding](<https://devfeed.tech/tags/coding.md>), [coding-style](<https://devfeed.tech/tags/coding-style.md>), [destructuring](<https://devfeed.tech/tags/destructuring.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-beginner](<https://devfeed.tech/tags/kotlin-beginner.md>), [programming-languages](<https://devfeed.tech/tags/programming-languages.md>), [programming-tips](<https://devfeed.tech/tags/programming-tips.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>)

### AI overview

A Kotlin tutorial covering string templates, raw multiline strings, destructuring declarations, and sealed classes. It notes that destructuring can be risky when a class member order changes.

### Source excerpt

Part 2 of 'Cooking Tasty Code'. Dive deeper into Kotlin features like delegation, operator overloading, and more to enhance your coding style.

## The Dog Riddle

DevFeed: [The Dog Riddle](<https://devfeed.tech/articles/the-dog-riddle-27084.md>)

Original publisher: [Read original article](<https://zsmb.co/the-dog-riddle/>)

Author: Márton Braun

Published: 2019-04-25T18:00:00Z

Content type: article

Language: en

Sources: [zsmb.co](<https://devfeed.tech/sources/zsmb-co.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [API](<https://devfeed.tech/topics/api.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [api](<https://devfeed.tech/tags/api.md>), [api-design](<https://devfeed.tech/tags/api-design.md>), [code](<https://devfeed.tech/tags/code.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [marton-braun](<https://devfeed.tech/tags/marton-braun.md>), [sealed](<https://devfeed.tech/tags/sealed.md>), [sealed-class](<https://devfeed.tech/tags/sealed-class.md>), [zsmb](<https://devfeed.tech/tags/zsmb.md>), [zsmb-co](<https://devfeed.tech/tags/zsmb-co.md>), [zsmb13](<https://devfeed.tech/tags/zsmb13.md>), [zsmbco](<https://devfeed.tech/tags/zsmbco.md>)

### AI overview

A Kotlin API design challenge asks readers to model cats and dogs, including dogs with optional breeds, while enforcing valid syntax and predictable instance creation. The article discusses submitted solutions, a common singleton-related pitfall, sealed hierarchies, and exhaustive expressions.

### Source excerpt

A fun little challenge in Kotlin API design. Give it a try!