pablisco
02/23/2022, 6:03 PMGrégory Lureau
02/24/2022, 9:17 AMpablisco
02/24/2022, 10:33 AMpablisco
02/24/2022, 10:35 AMGrégory Lureau
02/24/2022, 11:10 AMephemient
02/24/2022, 11:36 AMpablisco
02/24/2022, 12:51 PMsealed interface Result<out T> {
value class Success<T>(
val value: T,
) : Result<T>
value class Failure(
val error: Throwable, // or custom
) : Result<Nothing>
}
Kotlin 1.7 will allow for generics on value classes so this kind of pattern will be even more powerful 🙂pablisco
02/24/2022, 12:54 PMsealed interface: Lce<out L, out C, out E> {
value class Loading<L>(val query: L) : Lce<L, Nothing, Nothing>
value class Content<C>(val content: C) : Lce<Nothing, C, Nothing>
value class Error<E>(val error: E) : Lce<Nothing, Nothing, E>
}
Although, if we don’t need a query for loading and we are happy with exceptions this is the same as a typealias of Result<T>?
🙂ephemient
02/24/2022, 1:20 PMGrégory Lureau
02/24/2022, 1:28 PMephemient
02/24/2022, 2:07 PMLoading
value with type Lce
, then it gets auto-boxed. just like how
val x: Int = 1
val y: Number = x
will auto-box Int
ephemient
02/24/2022, 2:09 PMpablisco
02/24/2022, 4:03 PMephemient
02/24/2022, 4:04 PMephemient
02/24/2022, 4:06 PM@JvmInline value class Foo(val foo: Any)
val x = Foo("hello")
x as Any == x as Any // true
x as Any === x as Any // false, because of boxing
ephemient
02/24/2022, 4:07 PMval y = 1000
y as Any == y as Any // true
y as Any === y as Any // false, because of boxing