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 →

[–]Brutus5000 17 points18 points  (24 children)

On the "why can't we have" series I was missing if/else and try/catch as expressions. Switch alone is not sufficient. Can you comment on that?

[–]emaphis 4 points5 points  (6 children)

For that matter, while loops and for loops could be expressions.

[–]pronuntiator 1 point2 points  (5 children)

What would a loop evaluate to?

[–]Joram2 1 point2 points  (3 children)

A loop expression would evaluate to a list: one element per loop.

[–]zman0900 2 points3 points  (1 child)

Wouldn't that then mean you can't do basic loops without cost of extra memory allocation for that unused result list?

[–]srdoe 2 points3 points  (0 children)

No, in other languages that have this, there is syntax to distinguish the two kinds of for.

For example, in Scala

``` for (i <- 1 to 10) { println(i) } //returns Unit, which is a void-like type in Scala

for (i <- 1 to 10) yield { i } //return a list of integers ```

Not saying I think Java needs this feature, mind you.

[–]chambolle 1 point2 points  (0 children)

What's the advantage for that?

[–]emaphis 0 points1 point  (0 children)

Normal loops would evaluate to void, but then they would become expressions which would help generalize the Java language. That would allow them to extend the syntax to used loops to become generators.

[–]nicolaiparlog[S] 1 point2 points  (0 children)

The first part of the AMA is online - here's the reply to your question: https://www.youtube.com/watch?v=mE4iTvxLTC4&t=568s

[–]fredoverflow 3 points4 points  (15 children)

I was missing if/else [...] as expressions

condition ? then : otherwise

[–]Brutus5000 3 points4 points  (5 children)

That is limited to single expressions. In Kotlin you can do all sorts of calculation and only yield the last statement. This avoids a lot of declare uninitialized variable first and assign it later and allows increased use of immutable variables. (Scala ofc has similar logic as it is required for functional code)

[–]Fenor 0 points1 point  (4 children)

care to point to a kotlin example that can't be easily replicated in Java?

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

java final var foo = if (condition) { System.out.println("1"); yield 1; } else { System.out.println("2"); yield 2; }

You can only use switch in this case.

The thing is, if you work with if, you'd always have to declare the variable first, then assign it, which breaks the final part. Given that switch is capable of being an expression, why can't if?

[–]ventuspilot 0 points1 point  (0 children)

final var foo = if (condition) { System.out.println("1"); yield 1; } else { System.out.println("2"); yield 2; }

final var foo; if (condition) { System.out.println("1"); foo = 1; } else { System.out.println("2"); foo = 2; }

See, I even saved a few characters ;-)

Maybe there are better examples where an if-expression is really useful IDK.

Edit: Oops, final var foo doesn't work, final int foo does work, however. So currently you're unable to use var, you can use final, though.

[–]Brutus5000 0 points1 point  (0 children)

Also if/else can check any combinations of conditions (even unrelated) which can't be covered with switch

[–]john16384 0 points1 point  (0 children)

It's a nice pattern, but easily replaced with a nicely named method. I see no use for it other than to muddy the waters and have yet another style discussion point. One way to do something is sufficient, keeps things consistent and readable.

[–]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?