https://kotlinlang.org logo
Title
l

Lilly

09/06/2021, 11:32 AM
Is there some flow operator to make this less verbose?:
val response = readPacketFlow().first { response -> response is Acknowledgement }
when (response) {
	Acknowledgement -> true
	else -> false
}
Edit: I would like to return true or false from flow if the first item matches a given predicate.
o

Orhan Tozan

09/06/2021, 11:35 AM
I think that codepiece isn't correct. Response can never be anything other than Acknowledgment, because if the flow cant find Acknowledgment, it will throw an exception
So response is of type Acknowledgment
l

Lilly

09/06/2021, 11:45 AM
True. I would like to return true or false from flow if the first item matches a given predicate. So if the first item is not of type
Acknowledgement
, it should return false
I guess this would be my best option:
return when (readPacketFlow().first()) {
	Acknowledgement -> true
	else -> false
}
👍 1
t

Tijl

09/06/2021, 11:51 AM
return readPacketFlow().first() is Acknowledgement
2