CompilerIf doesn't do short-circuit ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Martt
User
User
Posts: 15
Joined: Sat Dec 23, 2017 4:03 pm
Location: The Netherlands

CompilerIf doesn't do short-circuit ?

Post 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
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: CompilerIf doesn't do short-circuit ?

Post 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.
User avatar
Martt
User
User
Posts: 15
Joined: Sat Dec 23, 2017 4:03 pm
Location: The Netherlands

Re: CompilerIf doesn't do short-circuit ?

Post 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.
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: CompilerIf doesn't do short-circuit ?

Post 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
JagV12
New User
New User
Posts: 9
Joined: Sat Jun 22, 2019 11:28 am

Re: CompilerIf doesn't do short-circuit ?

Post 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 ?
Fred
Administrator
Administrator
Posts: 16687
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: CompilerIf doesn't do short-circuit ?

Post 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).
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: CompilerIf doesn't do short-circuit ?

Post 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. Image
Thanks for that!
User avatar
Martt
User
User
Posts: 15
Joined: Sat Dec 23, 2017 4:03 pm
Location: The Netherlands

Re: CompilerIf doesn't do short-circuit ?

Post 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.
Post Reply