Mark
03/01/2022, 2:30 AMval foos: List<Foo> = ...
foos.takeIf { it.isNotEmpty() }
to a function reference
1️⃣ foos.takeIf(List<Foo>::isNotEmpty)
2️⃣ foos.takeIf(List<*>::isNotEmpty)
3️⃣ foos.takeIf(Collection<Foo>::isNotEmpty)
4️⃣ foos.takeIf(Collection<*>::isNotEmpty)
5️⃣ leave it as a lambdagildor
03/01/2022, 2:46 AMephemient
03/01/2022, 3:00 AM{ }
lambdagildor
03/01/2022, 3:02 AMMark
03/01/2022, 3:04 AMephemient
03/01/2022, 3:05 AMMark
03/01/2022, 3:05 AMgildor
03/01/2022, 3:06 AMephemient
03/01/2022, 3:08 AMMark
03/01/2022, 3:08 AMtakeUnless(String::isBlank)
because it just looks cleaner than using a lambda, but I wish I could do something like takeUnless(it::isBlank)
or takeUnless(ITEM::isBlank)
or even with some compiler magic takeUnless.isBlank()
Javier
03/01/2022, 7:50 AMSam
03/01/2022, 9:20 AMfoos.ifEmpty { null }
Michael de Kaste
03/01/2022, 12:57 PMephemient
03/01/2022, 4:01 PMfoos.takeIf { it: List<Foo> -> it.isNotEmpty() }
but I think that's a poor way to do a compile-time type assertion. it would be clearer to
val foos: List<Foo> = ...
in the first placeMichael de Kaste
03/01/2022, 4:17 PM::
whenever possible