How to call functions inside ternary operators in UnityScript? - unity3d

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 !

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();

Related

Which if else code should I use, single line or multiple lines?

There are two types of if function codes:
condition1 ? function1 : condition2 ? function2 : function3;
if (condition1) {
function1
} else {
if (condition2) {
function2
} else {
function3
}
}
I don't know what is the correct way to use the if else function code, I hope everyone can tell me.
Feel free to leave a comment if you need more information.
Which if else code should I use, single line or multiple lines? I would appreciate any help. Thank you in advance!
Those two are not "different styles of if".
The first one is the ternary operator. As the term operator suggests, it is used to produce a value.
The second one is the flow control statement if. It does not produce any value, it changes your program's flow.
So which one should you use? The one that fits your goal best. Do you need a value? The operator. Do you need to change your program flow? The flow control statement.
Try to reproduce this if statement with a ternary operator:
if (trafficlight.current == red) {
stopVehicle();
}
You cannot. Not without adding pointless waste. Because this is flow control.
On the other hand, this:
var newSpeed = (trafficlight.current == red) ? 0 : this.MaxSpeed;
Would be very convoluted to write as an if statement. Because it is generating a value.
So pick what is best for your program. It's not a "style" to follow blindly. It is a decision you should make for every one of those instances.

How to implement non chronological backtracking

I'm working on a CDCL SAT-Solver. I don't know how to implement non-chronological backtracking. Is this even possible with recursion or is it only possible in a iterative approach.
Actually what i have done jet is implemented a DPLL Solver which works with recursion. The great differnece from DPLL and CDCL ist that the backracking in the tree is not chronological. Is it even possible to implement something like this with recursion. In my opionion i have two choices in the node of the binary-decision-tree if one of to path leads i a conlict:
I try the other path -> but then it would be the same like the DPLL, means a chronological backtracking
I return: But then i will never come back to this node.
So am i missing here something. Could it be that the only option is to implement it iterativly?
Non-chronological backtracking (or backjumping as it is usually called) can be implemented in solvers that use recursion for the variable assignments. In languages that support non-local gotos, you would typically use that method. For example in the C language you would use setjmp() to record a point in the stack and longjmp() to backjump to that point. C# has try-catch blocks, Lispy languages might have catch-throw, and so on.
If the language doesn't support non-local goto, then you can implement a substitute in your code. Instead of dpll() returning FALSE, have it return a tuple containing FALSE and the number of levels that need to be backtracked. Upstream callers decrement the counter in the tuple and return it until zero is returned.
You can modify this to get backjumping.
private Assignment recursiveBackJumpingSearch(CSP csp, Assignment assignment) {
Assignment result = null;
if (assignment.isComplete(csp.getVariables())) {
result = assignment;
}
else {
Variable var= selectUnassignedVariable(assignment, csp);
for (Object value : orderDomainValues(var, assignment, csp)) {
assignment.setAssignment(var, value);
fireStateChanged(assignment, csp);
if (assignment.isConsistent(csp.getConstraints(var))) {
result=recursiveBackJumpingSearch(csp, assignment);
if (result != null) {
break;
}
if (result == null)
numberOfBacktrack++;
}
assignment.removeAssignment(var);
}
}
return result;
}

How do you write an empty while loop in Coffeescript?

I'm trying to translate some old code to Coffeescript. But there is no direct translation for:
while ( doWork() ) {}
"while doWork()" with nothing after it results in a syntax error.
while doWork() then
Should do the trick
using then is probably the canonical solution since it is explicitly meant for separating the condition from the (in this case empty) body. Alternatively you can write
while doWork()
;#
(the # keeps vim syntax highlighting from flagging it as an error)
I also like the continue while doWork() solution, but I strongly advise against any other form of expression while doWork() mentioned in the comments since when this is the last statement of a function it will become a list constructor:
_results = [];
while (doWork()) {
_results.push(expression);
}
return _results;

ternary in postlude?

Can I do ternary in the postlude(s) the same way that I do them int he prelude? In other words have it fire off different calls depending on the say something was evaluated in the prelude?
Yes. You can include a guard condition on a postlude expression. This works for both explicit event raising and persistant variable modification. The expression following if can be any valid expression, including functions.
Example:
fired {
raise explicit event "foo" if cheese == 5;
}

; expected but <place your favourite keyword here> found

I'm trying to write a class for a scala project and I get this error in multiple places with keywords such as class, def, while.
It happens in places like this:
var continue = true
while (continue) {
[..]
}
And I'm sure the error is not there since when I isolate that code in another class it doesn't give me any error.
Could you please give me a rule of thumb for such errors? Where should I find them? are there some common syntactic errors elsewhere when this happens?
It sounds like you're using reserved keywords as variable names. "Continue", for instance, is a Java keyword.
You probably don't have parentheses or braces matched somewhere, and the compiler can't tell until it hits a structure that looks like the one you showed.
The other possibility is that Scala sometimes has trouble distinguishing between the end of a statement with a new one on the next line, and a multi-line statement. In that case, just drop the ; at the end of the first line and see if the compiler's happy. (This doesn't seem like it fits your case, as Scala should be able to tell that nothing should come after true, and that you're done assigning a variable.)
Can you let us know what this code is inside? Scala expects "expressions" i.e. things that resolve to a particular value/type. In the case of "var continue = true", this does not evaluate to a value, so it cannot be at the end of an expression (i.e. inside an if-expression or match-expression or function block).
i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
}
This is a problem, as the function block is an expression and needs to have an (ignored?) return value, i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
()
}
() => a value representing the "Unit" type.
I get this error when I forget to put an = sign after a function definition:
def function(val: String):Boolean {
// Some stuff
}