Page 1 of 1
CompilerIf doesn't do short-circuit ?
Posted: Fri May 08, 2020 10:53 am
by Martt
PB does support short-circuit (
https://en.wikipedia.org/wiki/Short-circuit_evaluation). Very helpfull.
The following code in 5.71 and 5.72 does not work (constant not found: #Test):
Code: Select all
;#Test = #True
CompilerIf Defined(Test, #PB_Constant) And #Test
Debug "Hi there"
CompilerEndIf
Is there a reason ? Solvable of course by using a nested CompilerIf...
Code: Select all
;#Test = #True
CompilerIf Defined(Test, #PB_Constant)
CompilerIf #Test
Debug "Hi there"
CompilerEndIf
CompilerEndIf
Re: CompilerIf doesn't do short-circuit ?
Posted: Fri May 08, 2020 12:18 pm
by wombats
A semicolon in PureBasic is used to denote a comment. You don't need them in front of constants. Remove it and it'll work.
Re: CompilerIf doesn't do short-circuit ?
Posted: Fri May 08, 2020 12:31 pm
by Martt
wombats wrote:A semicolon in PureBasic is used to denote a comment. You don't need them in front of constants. Remove it and it'll work.
Of course it works without the semicolon. The whole point is to check if a constant exists (part 1). If it
doesn't exist the second part should not be checked.
That is what short-circuit is all about. Since part 1 is zero, the
And cannot possibly become true. So with short-circuit the second part will never be checked.
Re: CompilerIf doesn't do short-circuit ?
Posted: Fri May 08, 2020 1:40 pm
by wombats
Well, this does the same:
Code: Select all
If 1 + 1 = 3 And #Test
Debug "Hi there"
EndIf
Maybe it's a bug?
Running the following on macOS doesn't output "Hi there":
Code: Select all
#Test = #True
CompilerIf #PB_Compiler_OS = #PB_OS_Windows And #Test
Debug "Hi there"
CompilerEndIf
Re: CompilerIf doesn't do short-circuit ?
Posted: Fri May 08, 2020 3:19 pm
by JagV12
@wombat : of course it doesn't output "Hi there" but that's not the point here. AFAI understand Martt's point, if the first term of an AND statement in a CompilerIf evaluates as False, the second term shouldn't raise an error at compile time because of Short-circuit_evaluation. But it does here as shown with his first example code.
@Martt : obviously no Short-circuit_evaluation at compile time. a)Did it work with older PB versions ? b)What don't you like (what is it you don't like ?) with nested CompileIf ?
Re: CompilerIf doesn't do short-circuit ?
Posted: Fri May 08, 2020 3:35 pm
by Fred
The whole line is always parsed and expected to be syntax correct before the evaluation begins. It is to avoid spelling mistake for example (if you had used #Testt instead, it would work as well but wouldn't do what you were expecting).
Re: CompilerIf doesn't do short-circuit ?
Posted: Fri May 08, 2020 3:49 pm
by Little John
Fred wrote:The whole line is always parsed and expected to be syntax correct before the evaluation begins. It is to avoid spelling mistake for example (if you had used #Testt instead, it would work as well but wouldn't do what you were expecting).
Good idea, it's much appreciated.

Thanks for that!
Re: CompilerIf doesn't do short-circuit ?
Posted: Sat May 09, 2020 8:22 am
by Martt
@JagV12: I thought I tested this a while ago, but v5.62 also doesn't do it. Looks like it never worked that way. I allways try to use one line If's. Cleaner code.
@Fred: Thanks for the explanation.