Antanas A.
06/14/2019, 6:49 AMinline fun <T> T.takeUnless
would work lazily and short circuit code execution if condition is true, kind of lazy evaluation of previous expression
. What's your thoughts? Is it possible at all?gildor
06/14/2019, 6:59 AMPavlo Liapota
06/14/2019, 7:17 AMkind of lazy evaluation ofprevious expression
takeUnless
returns null
if condition is true
.
But I guess what you want is just
if (condition) null else foo()
Soo foo()
is evaluated only if condition is false
.
And of course you can create extension function with a condition as a receiver, if it makes sense for you.Antanas A.
06/14/2019, 7:27 AMtakeUnless
passes a value into predicate functionfun <T> takeUnless(predicate: Boolean, block: () -> T): T? = if (!predicate) block() else null
val event = GeneratedEvent(
on = true,
timestamp = randomize(randomSeed, max(visibleRange.start, interval.start - RandomizationRange), interval.start),
totalVehicleMiles = dutyEvent.totalVehicleMiles
).takeUnless { interval.start == visibleRange.start }
gildor
06/14/2019, 7:34 AMval event = if (interval.start != visibleRange.start) {
GeneratedEvent(…)
} else {
null
}
Antanas A.
06/14/2019, 7:34 AMDico
06/15/2019, 5:58 AM