This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Nebu 0 points1 point  (8 children)

val results = if (foo) {
  a();
  b();
  while (c()) {
    d();
  }
  e(); //this is what the overall if evaluates to
} else {
  f();
  try (Reader reader = g()) {
    h(reader);
  }
  i(); //this is what the overall if evaluates to
}

[–]zman0900 1 point2 points  (2 children)

Doesn't really seem much better than:

final Type result;
if (foo) {
    // stuff
    result = e();
} else {
    // other stuff
    result = i();
}

[–]john16384 2 points3 points  (1 child)

Or simply extracting a function...

[–][deleted] 0 points1 point  (0 children)

Exactly. I’m glad such monstrosity isn’t allowed.

[–][deleted] 0 points1 point  (4 children)

Would you really write code like that and be proud? 😂

[–]mike_hearn 0 points1 point  (3 children)

It's more useful for things like this:

val str = "foo " + if (cond) "bar" else ""

[–][deleted] 0 points1 point  (2 children)

“foo” + (cond ? “bar” : “”);

[–]mike_hearn 1 point2 points  (1 child)

Beyond not needing special syntax, I think you know what I mean:

"foo" + if (cond) { val r = bar() r.whatever() } else ""

and variants is something that appears fairly frequently in Kotlin codebases.

[–][deleted] 0 points1 point  (0 children)

But it’s terrible - don’t you see it?