bodiam
10/23/2020, 1:03 AMdata class Alert(val message: String)
val alerts : List<Optional<Alert>> = getAlerts()
// filter
val messages = alerts
.filter { it.isPresent }
.map {it.get().message }
Nir
10/23/2020, 1:52 AMAlert?
Nir
10/23/2020, 1:54 AMval alerts : List<Alert?>
then you couldNir
10/23/2020, 1:54 AMval messages = alerts.mapNotNull { it?.message }
Fady Emad
10/23/2020, 2:00 AMfun <T> Optional<T>.unwrap(): T? = orElse(null)
Then use it like you wanted:
val msg: Something? = optional.unwrap()
bodiam
10/23/2020, 2:01 AMNir
10/23/2020, 2:02 AMNir
10/23/2020, 2:02 AMbodiam
10/23/2020, 2:04 AMNir
10/23/2020, 2:05 AMNir
10/23/2020, 2:05 AMNir
10/23/2020, 2:05 AMAlert?
, not Optional<Alert>
Nir
10/23/2020, 2:06 AMAlert?
where you need an Alert
bodiam
10/23/2020, 2:08 AMFady Emad
10/23/2020, 2:09 AMbodiam
10/23/2020, 2:10 AMNir
10/23/2020, 2:11 AMAlert?
? Leaving aside the fact that the code is mixed.bodiam
10/23/2020, 2:11 AMNir
10/23/2020, 2:11 AMNir
10/23/2020, 2:11 AMAlert?
, and none for Optional<Alert>
Nir
10/23/2020, 2:13 AMval messages = alerts
.mapNotNull { it.unwrap()?.message }
bodiam
10/23/2020, 2:14 AMNir
10/23/2020, 2:14 AMNir
10/23/2020, 2:14 AMNir
10/23/2020, 2:15 AMNir
10/23/2020, 2:15 AMAlert?
bodiam
10/23/2020, 2:16 AMNir
10/23/2020, 2:17 AMbodiam
10/23/2020, 2:18 AMNir
10/23/2020, 2:18 AMNir
10/23/2020, 2:18 AMNir
10/23/2020, 2:18 AMbodiam
10/23/2020, 2:18 AMbodiam
10/23/2020, 2:19 AMNir
10/23/2020, 2:19 AMNir
10/23/2020, 2:19 AMbodiam
10/23/2020, 2:20 AMNir
10/23/2020, 2:20 AMTristan Blakers
10/23/2020, 2:20 AMNir
10/23/2020, 2:20 AMNir
10/23/2020, 2:20 AMTristan Blakers
10/23/2020, 2:20 AMfun Optional<T>.unWrap() : T?
Nir
10/23/2020, 2:21 AMTristan Blakers
10/23/2020, 2:22 AMTristan Blakers
10/23/2020, 2:22 AMbodiam
10/23/2020, 2:22 AMunwRap?
Tristan Blakers
10/23/2020, 2:22 AMfun Optional<T>.unwrap() : T?
😄Nir
10/23/2020, 2:22 AMNir
10/23/2020, 2:22 AMTristan Blakers
10/23/2020, 2:23 AMNir
10/23/2020, 2:23 AMNir
10/23/2020, 2:24 AMDeprecated: Option will be deleted soon as it promotes the wrong message of using a slower and memory unfriendly abstraction when the lang provides a better one.@higherkind sealed class ~~Option~~<out A> : OptionOf<A>
Tristan Blakers
10/23/2020, 2:24 AMNir
10/23/2020, 2:24 AMNir
10/23/2020, 2:25 AMTristan Blakers
10/23/2020, 2:25 AMTristan Blakers
10/23/2020, 2:25 AMNir
10/23/2020, 2:26 AMTristan Blakers
10/23/2020, 2:26 AMNir
10/23/2020, 2:26 AMNir
10/23/2020, 2:27 AMTristan Blakers
10/23/2020, 2:28 AMobject MyError
fun myFunc(): Either<MyError, T> = nullableFunc().rightIfNotNull { MyError }
Tristan Blakers
10/23/2020, 2:29 AMNir
10/23/2020, 2:29 AMNir
10/23/2020, 2:30 AMNir
10/23/2020, 2:30 AMTristan Blakers
10/23/2020, 2:31 AMeither {
val a = myEitherFun().bind()
val b = anotherEitherFun(a).bind()
}
etcNir
10/23/2020, 2:31 AMNir
10/23/2020, 2:32 AM.bind
cause early exit from the either
block with that errorTristan Blakers
10/23/2020, 2:32 AMTristan Blakers
10/23/2020, 2:32 AMNir
10/23/2020, 2:33 AMTristan Blakers
10/23/2020, 2:33 AMNir
10/23/2020, 2:34 AMTristan Blakers
10/23/2020, 2:34 AMNir
10/23/2020, 2:34 AMeither
?Tristan Blakers
10/23/2020, 2:34 AMNir
10/23/2020, 2:37 AMNir
10/23/2020, 2:37 AMNir
10/23/2020, 2:38 AMTristan Blakers
10/23/2020, 2:38 AMval a = nullable()
val b = a.let { nullable2() }
val c = nullable3()
// want to add b + c, but not really clean
// val d = b + c
Nir
10/23/2020, 2:39 AMTristan Blakers
10/23/2020, 2:40 AMNir
10/23/2020, 2:40 AMTristan Blakers
10/23/2020, 2:40 AMNir
10/23/2020, 2:40 AMlet
is built into the language, you can write a new function that works exactly the way either
works, but for nullableTristan Blakers
10/23/2020, 2:41 AMNir
10/23/2020, 2:41 AMeither
implemenhtation and change it to work for nullable types. Are you asserting that's not possible?Nir
10/23/2020, 2:41 AMTristan Blakers
10/23/2020, 2:42 AMNir
10/23/2020, 2:42 AMTristan Blakers
10/23/2020, 2:44 AMNir
10/23/2020, 2:45 AMval x = runCatching {
val a = nullable()!!
val b = ....
}
Nir
10/23/2020, 2:45 AMTristan Blakers
10/23/2020, 2:45 AMNir
10/23/2020, 2:45 AMNir
10/23/2020, 2:46 AMNir
10/23/2020, 2:46 AMNir
10/23/2020, 2:47 AMNir
10/23/2020, 2:47 AMNir
10/23/2020, 2:47 AMTristan Blakers
10/23/2020, 2:51 AMNir
10/23/2020, 2:52 AMNir
10/23/2020, 2:52 AMNir
10/23/2020, 3:12 AM