tipsy
07/10/2019, 2:38 PMusually({
}, sometimes {
})
is it possible to get rid of the parens for usually
somehow, so i just get:
usually {
} sometimes {
}
?Adam Powell
07/10/2019, 2:40 PMusually
return something and sometimes
an infix function with the UsuallyResult
on the left hand sidetipsy
07/10/2019, 2:40 PMwbertan
07/10/2019, 2:41 PMfun usually(block: () -> Unit) = block()
infix fun Unit.sometimes(block: () -> Unit) = block()
@Test
fun asas() {
usually {
} sometimes {
}
}
tipsy
07/10/2019, 2:43 PMtipsy
07/10/2019, 2:43 PMtipsy
07/10/2019, 2:53 PMdata class UsuallyResult(val usually: () -> Unit) {
infix fun sometimes(sometimes: () -> Unit) {
if (Math.random() < 0.8) {
this.usually();
} else {
sometimes();
}
}
}
fun usually(usually: () -> Unit) = UsuallyResult(usually)
fun main() {
usually {
println("This should usually happen")
} sometimes {
println("But, sometimes this happens!")
}
}
tipsy
07/10/2019, 2:53 PMkarelpeeters
07/10/2019, 4:25 PMkarelpeeters
07/10/2019, 4:26 PMtipsy
07/10/2019, 4:26 PMtipsy
07/10/2019, 4:27 PMtipsy
07/10/2019, 4:38 PMtipsy
07/10/2019, 4:38 PMkarelpeeters
07/10/2019, 4:38 PMtipsy
07/10/2019, 4:38 PMMath.random
everywhere, so i wanted to wrap it in a neat function