-4

Java has ternary operator, that works on variables:

(a > 0) ? b++ : c--;

What is a, b, c were functions? I tested something like (a returns boolean, b & c are void):

a() ? b() : c(); 

but even my Eclipse does not allow that.

How close could I get to this programmatically (to generate functionality to make this with minimum amount of lines)?

I know in other languages like Javascript I could just pass function as parameter, but I have not met similar in Java.

Edit

We have gone in discussion to sidewals because of bad constellation of question, but here is what I want, and it is a real world problem:

What I want to achieve is single-liner to call second or third function based on return value of the first.

With minimum amount of code of course.

What I had tried was that ternary that is made to other things, that I know now.

mico
  • 12,730
  • 12
  • 59
  • 99
  • 5
    You can only use methods that return a value as operands of the ternary conditional operator, since that operator must produce a value. – Eran Jul 31 '18 at 10:08
  • 3
    If a() returns a boolean and b() and c() return the type that shall be assigned respectively, it should work. – TheRealHypo Jul 31 '18 at 10:09
  • However if the target is a funtional interface lambdas are a thing. – L.Spillner Jul 31 '18 at 10:09
  • 2
    The ternary operator is not a replacement for the `if` statement. It is supposed to evaluate expressions, not execute arbitrary statements. If you used methods with return types, `a() ? b() : c()` would be legal, because it could be _evaluated_, not just _executed_. Otherwise, what you want is an `if` statement. – khelwood Jul 31 '18 at 10:09
  • 2
    what about `if (a()) b(); else c();`? – king_nak Jul 31 '18 at 10:09
  • 1
    *"my Eclipse does not allow that"* - Why not? Is there an error? What is that error? How have you confirmed that error is because of the conditional operator? What testing and experimentation have you done to validate this? *"I know in other languages like Javascript I could just pass function as parameter"* - That's true, but what does that have to do with the conditional operator? I think you're making some incorrect assumptions or have some fundamental misunderstandings about what you're trying to do. – David Jul 31 '18 at 10:11
  • @king_nak had the solution I was asking for. Other aspects like "should this be valid Java" about the original question were some sort of story behind, Your answer was the thing what I was after: alternative one-liner for functions. – mico Jul 31 '18 at 10:39
  • @khelwood regardless of whether `b` and `c` have return types, `a() ? b() : c();` (note the semicolon, which OP used but you did not) is never legal. – Michael Jul 31 '18 at 10:42
  • I know now for sure (thanks to the amount of comments and my research before asking) the original a() ? b() ? c() is bullshit, but I wanted to know what is the oneliner, if a() return value tells to either call b() or c() and I wanted to do it many times. – mico Jul 31 '18 at 10:45
  • (a() -> b() or c() then later a() -> d() or e(). You see, this makes many letters in code easily – mico Jul 31 '18 at 10:50
  • @Michael That's true. `a() ? b() : c();` is not legal as a statement, but `a() ? b() : c()` might be legal as an expression. – khelwood Jul 31 '18 at 10:55
  • It seems to be, that I am on wrong forum? CodeReview or CodeGolf, should there be more people towards this kind of questions? – mico Jul 31 '18 at 11:13

3 Answers3

1

If you go through Java Ternary Operator let's you assign a value to a variable based on a boolean expression — either a boolean field, or a statement that evaluates to a boolean result (Its used for assignment). In above you are trying to call functions with return value void that is not allowed in Ternary Operator in java.

The ternary operator, also known as the conditional operator, can be used as an alternative to the Java if/then/else syntax

Ravindra Kumar
  • 1,842
  • 14
  • 23
1

As others have already explained, the conditional operator is an expression that evaluates to a value, and can thus not be applied to void methods.

If you need a one-liner discarding any return values, use a simple if-else:

if (a()) b(); else c();
king_nak
  • 11,313
  • 33
  • 58
0

You can only use the ternary operator as an expression, not a statement. It's not simply alternative syntax for an if-else - the point is that if a() returns true, the entire expression resolves to the result of b(), else the entire expression resolves to the result of c().

As such, it doesn't make sense if b and c return void. They must return something, and the most specific type that they share will be the resulting type of the whole expression.

Michael
  • 41,989
  • 11
  • 82
  • 128