Coming from Java, I was surprised to find out that Kotlin doesn't allow assignments as expressions.
Is there a reason for that?
Java (Works)
@Test
public void test_x() {
List<String> elements = null;
for (final String x : (elements = List.<String>of("1", "2"))) {
System.out.println(x);
}
}
Kotlin (Compile-time error)
/*Assignments are not expressions, and only expressions are allowed in this context*/
@Test
fun test_x() {
var elements: List<String> = listOf()
for (x in (elements = listOf("1", "2"))) {
println(x)
}
}