joelpedraza
09/27/2017, 1:59 PMfinal boolean b;
final double d;
{
final Random rng = new Random();
final double r = rng.nextDouble();
if (r < 0.5d) {
b = false;
d = r / 2;
} else {
b = true;
d = r * 3;
}
// rng and r fall out of scope
}
Is it possible to express this in kotlin? using a run {} "block" is a lambda, and captured value initialization is forbidden.marstran
09/27/2017, 2:03 PMinit
block.val b:Boolean
val d:Double
init {
val rng = Random()
val r = rng.nextDouble()
if (r < 0.5) {
b = false
d = r / 2
} else {
b = true
d = r * 3
}
}
joelpedraza
09/27/2017, 2:06 PMmarstran
09/27/2017, 2:07 PMneil.armstrong
09/27/2017, 2:08 PMfun main(args: Array<String>) {
var b: Boolean
var d: Double
{
val rng = Random()
val r = rng.nextDouble()
if (r < 0.5) {
b = false
d = r / 2
} else {
b = true
d = r * 3
}
}()
}
joelpedraza
09/27/2017, 2:09 PMmarstran
09/27/2017, 2:10 PMjoelpedraza
09/27/2017, 2:10 PMneil.armstrong
09/27/2017, 2:10 PMmarstran
09/27/2017, 2:10 PMvar
.rng
and r
to leave the scope?joelpedraza
09/27/2017, 2:12 PM