0

I'm trying to decide which function to call, based on a boolean value.

myBooleanVariable ? function1() : function2();

Unity gives the error :

Expressions in statements must only be executed for their side-effects.

So why does this not work, and how can I make it work ?

Thanks for any help !

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
jeff
  • 13,055
  • 29
  • 78
  • 136
  • 1
    Something tells me you've left an important word out of that error message *("...must **not** be...")*. – T.J. Crowder Dec 16 '13 at 07:09
  • It works for me, can you provide more details. – Asterisk Dec 16 '13 at 07:10
  • Does your functions return anything? – A Paul Dec 16 '13 at 07:10
  • @Asterisk: Are you using UnityScript? (Unity's implementation of JavaScript) – T.J. Crowder Dec 16 '13 at 07:11
  • @T.J.Crowder I copy pasted the error message. [@]ABP no, they are void functions. And I'm using a JavaScript file inside Unity, I don't know if it's called UnityScript. – jeff Dec 16 '13 at 07:14
  • @CengizFrostclaw: Then there's almost certainly a typo in Unity. :-) It makes no sense as it is. – T.J. Crowder Dec 16 '13 at 07:16
  • @T.J.Crowder I agree with you. But still not sure what is meant by side-effects, and I have no idea what is an expression and what is a statement, so I cannot comment any further :) – jeff Dec 16 '13 at 07:18
  • 1
    @CengizFrostclaw: A [side-effect](http://en.wikipedia.org/wiki/Side_effect_(computer_science)) is a result of a function that isn't its return value. You're using the conditional operator to call one of two functions, and its complaining (I *think*) because although you're using an operator (in an expression), you're not doing anything with the return value, and so only using it to get the side effects. An *expression* is something that can be used as a right-hand value (the right-hand side of an assignment, passed into a function, etc.); *statement* is harder to define concisely. :-) – T.J. Crowder Dec 16 '13 at 07:22

1 Answers1

2

So why does this not work, and how can I make it work ?

If it's true that it doesn't work (I don't have Unity to hand), it means UnityScript (Unity's implementation of JavaScript) doesn't support the expression statement. Which puts it at variance with the specification, and means a fair number of JavaScript idioms won't work in it. Your line is perfectly valid JavaScript/ECMAScript. You might check to see if there are "lint"-style options you can enable/disable.

The solution would be to use the result of the expression, or rewrite that using if.

Use the result:

var f = myBooleanVariable ? function1() : function2();

Using if:

if (myBooleanVariable) {
    function1();
}
else {
    function2();
}

Or if you really want the if to be on one line:

if (myBooleanVariable) function1(); else function2();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Yep, I did as your last line. I just wanted to know if I'm skipping something about ternary operators. Thanks ! – jeff Dec 16 '13 at 07:16
  • 1
    @CengizFrostclaw: You're not in true *JavaScript*, no; your "ternary" line (more properly, your "[conditional expression](http://www.ecma-international.org/ecma-262/5.1/#sec-11.12)" line) works just fine in true JavaScript. – T.J. Crowder Dec 16 '13 at 07:17
  • ok then, I will blame Unity :) Thanks again ! accepting in 1 min. – jeff Dec 16 '13 at 07:19
  • 2
    It might also be a (sane) style control, which would then probably be configurable somewhere (never tried Unity, so pure speculation). – Denys Séguret Dec 16 '13 at 07:19
  • @JanDvorak Some IDE and stuff let you define what problems should render as warnings or errors. Personally I don't like warnings, they accumulate. Code should just be right or wrong. – Denys Séguret Dec 16 '13 at 07:21
  • @dystroy tell this to Android developers :) – jeff Dec 16 '13 at 07:26