Rules if-then-else and fall-through
Posted: 25 May 2018, 13:33
I over-estimated what the rules parser was capable of and found an interesting effect that could cause problems if one were to make a mistake by leaving out an else or endif... this doesn't feel like intentional behaviour and isn't logical, so it may need some work to stop it happening.
I wrote:
on Rules#Timer=1 do
if [dummy#var1] = 1
7dt,1
TaskValueSet,12,1,2
endif
if [dummy#var1] = 2
7dt,2
TaskValueSet,12,1,1
endif
endon
I expected, logically, the display to alternate between showing "1" and "2" each time the timer event occurred.
However, what happens in fact is that each time the event occurs with dummy#var1 = 1, the display shows "1" very briefly and then stops on "2". The code execution is falling through from the first "if" clause and executing the code inside the second "if" clause.
I get that the rules are very simple logic and the intended coding would be to nest the second "if" in an "else" section of the first "if" - although that will produce some very ugly code in an application like mine where there are 7 different display values to cycle through - but the problem here is that the parser fell through and executed code that shouldn't have been executed. That could happen easily if someone missed an "else" or "endif" in otherwise valid code.
Could the parser be amended so that it gives an error or ignores the second "if" clause (or better still, processes it properly)?
I wrote:
on Rules#Timer=1 do
if [dummy#var1] = 1
7dt,1
TaskValueSet,12,1,2
endif
if [dummy#var1] = 2
7dt,2
TaskValueSet,12,1,1
endif
endon
I expected, logically, the display to alternate between showing "1" and "2" each time the timer event occurred.
However, what happens in fact is that each time the event occurs with dummy#var1 = 1, the display shows "1" very briefly and then stops on "2". The code execution is falling through from the first "if" clause and executing the code inside the second "if" clause.
I get that the rules are very simple logic and the intended coding would be to nest the second "if" in an "else" section of the first "if" - although that will produce some very ugly code in an application like mine where there are 7 different display values to cycle through - but the problem here is that the parser fell through and executed code that shouldn't have been executed. That could happen easily if someone missed an "else" or "endif" in otherwise valid code.
Could the parser be amended so that it gives an error or ignores the second "if" clause (or better still, processes it properly)?