Page 1 of 2

Bool() use?

Posted: Tue Jan 22, 2013 7:17 am
by MachineCode
I just tried PureBasic 5.10 and this snippet no longer works: result = num1 And num2. It says I have to use Bool(), but there's no Help file yet. (I can guess and assume how it works, but I'd like to know for sure, so I don't write incorrect code). ;)

Re: Bool() use?

Posted: Tue Jan 22, 2013 8:23 am
by Arctic Fox

Code: Select all

result = Bool(num1 And num2)
:)

The help entry for Bool() is in 'Compiler Functions'.

Re: Bool() use?

Posted: Tue Jan 22, 2013 9:22 am
by MachineCode
Arctic Fox wrote:The help entry for Bool() is in 'Compiler Functions'.
Thanks for showing me how to do it. I don't have a Help file that has Bool() in Compiler Functions (I don't have the latest beta yet).

Why would it go under Compiler Functions anyway? Seems odd. It's an everyday function, rather that something that tells the compiler how to act.

Also, I take it that "result = a Or B" would now also become "result = Bool(a Or b)" ?

Re: Bool() use?

Posted: Tue Jan 22, 2013 12:40 pm
by luis
MachineCode wrote:Why would it go under Compiler Functions anyway?
Because it's not a runtime function, it's interpreted by the compiler at compilation time. It generates different code.
MachineCode wrote:Also, I take it that "result = a Or B" would now also become "result = Bool(a Or b)" ?
Yep.

Re: Bool() use?

Posted: Tue Jan 22, 2013 1:09 pm
by MachineCode
Thanks, Luis. :)

Re: Bool() use?

Posted: Tue Jan 22, 2013 11:52 pm
by flaith
MachineCode wrote:Also, I take it that "result = a Or B" would now also become "result = Bool(a Or b)" ?
Right, for example, if you want to try if a char is a digit or not :

Code: Select all

Procedure.i isDigit(__char.c)
  ProcedureReturn Bool(__char >= '0' And __char <= '9')
EndProcedure

Re: Bool() use?

Posted: Wed Jan 23, 2013 7:43 am
by BorisTheOld
luis wrote:
MachineCode wrote:Why would it go under Compiler Functions anyway?
Because it's not a runtime function, it's interpreted by the compiler at compilation time. It generates different code.
That has to be the silliest thing I've every read. My entire program is interpreted by the compiler at compile time and converted into machine code.

The internal workings of the compiler should not dictate the placement of material in the help file.

Re: Bool() use?

Posted: Wed Jan 23, 2013 8:02 am
by Little John
BorisTheOld wrote:
luis wrote:
MachineCode wrote:Why would it go under Compiler Functions anyway?
Because it's not a runtime function, it's interpreted by the compiler at compilation time. It generates different code.
That has to be the silliest thing I've every read.
That is not silly at all.
BorisTheOld wrote:My entire program is interpreted by the compiler at compile time and converted into machine code.
So what? I estimate that more than 99% of all programs written in PB additionally do some computations at runtime.

Regards, Little John

Re: Bool() use?

Posted: Wed Jan 23, 2013 8:21 am
by BorisTheOld
Little John wrote:Maybe. However, I estimate that more than 99% of all programs written in PB additionally do some computations at runtime.
But that's my point. When I write code in a high level language I don't care how the underlying code performs the actual function - at compile time or at run time. So when I code x=SizeOf(y) and x=Len(y), all I care about is the result not how it was obtained.

Which all begs the question, why are Bool, InitializeStructure, CopyStructure, and ClearStructure classified as Compiler Functions, when they do their work at run time?

Silly, isn't it? :)

Re: Bool() use?

Posted: Wed Jan 23, 2013 9:54 am
by netmaestro
why are Bool, InitializeStructure, CopyStructure, and ClearStructure classified as Compiler Functions
Because they are worker functions that cause the compiler to generate code for you that you would otherwise have to write. Bool() allows you to write one line instead of an If-Else-Endif block, CopyStructure allows for the one-line 'a.point = b.point' whereas without it you would have had to set each member individually, same for ClearStructure which would have you setting each element to zero, which gets quite involved with the recent addition of dynamic objects in structures. These are compiler functions all right, and I don't find them silly.

Re: Bool() use?

Posted: Wed Jan 23, 2013 12:37 pm
by MachineCode
I don't know about silly, but to me, Bool() should go in the Math lib, since it's performing a math calculation. Anyway, not trying to cause further argument about it. Just saying how it looks.

Re: Bool() use?

Posted: Wed Jan 23, 2013 5:27 pm
by Little John
BorisTheOld wrote:When I write code in a high level language I don't care how the underlying code performs the actual function - at compile time or at run time. So when I code x=SizeOf(y) and x=Len(y), all I care about is the result not how it was obtained.
Yes, for a similar reason I don't understand why there are all those discussions on nuclear power stations and whatnot. My power just comes out of the electric socket. :mrgreen:

Seriously:
It can matter, whether a function or command has an effect at compile time or at runtime. I'll demonstrate it by some simple examples.

Code: Select all

DataSection
   MapLabel:
   IncludeBinary "map.data"  ; file size: 50 MB
EndDataSection 
If IncludeBinary has an effect at compile time, then my EXE file will increase by 50 MB, and I don't have to ship the file "map.data" to the customers of my program.
If IncludeBinary has an effect at runtime, then my EXE file will not increase by 50 MB, but I have to ship the file "map.data" to the customers of my program.

Code: Select all

DataSection
   CompilerIf Date() > 1000
      MapALabel:
      IncludeBinary "mapA.data"
   CompilerElse
      MapBLabel:
      IncludeBinary "mapB.data"
   CompilerEndIf
EndDataSection

Since CompilerIf / CompilerElse has an effect at compile time, the last code can only work if Date() is also evaluated at compile time. If you know that Date() is a runtime function, then you immediately understand why this code can't work.

I hope you see now, why asking whether something happens at compile time or at runtime is a "natural" consideration when talking about compiled computer programs -- and why it can serve a purpose. Actually, I expect a good documentation to tell me whether a given function or command has an effect at compile time or at runtime. If you personally are not interested in this information, you are free to ignore it. :-)

The original problem here in this thread was, that the help for Bool() could not be found. The reason for this was not any principle problem related to compile time or runtime, but just a small bug in the help file. It has been fixed, and all is fine now, I think. :-)

Regards, Little John

Re: Bool() use?

Posted: Wed Jan 23, 2013 7:15 pm
by SFSxOI
flaith wrote:
MachineCode wrote:Also, I take it that "result = a Or B" would now also become "result = Bool(a Or b)" ?
Right, for example, if you want to try if a char is a digit or not :

Code: Select all

Procedure.i isDigit(__char.c)
  ProcedureReturn Bool(__char >= '0' And __char <= '9')
EndProcedure
Or.....

Code: Select all

Macro isDigit(__char)
  (__char >= '0' And __char <= '9')
EndMacro

Debug isDigit('0')
Debug isDigit('9')
:)

Anyway... just curious, but what exactly does 'Bool' do that can't already be done with a macro or a 'If - EndIf' statement arangement or a reqular expression or some other comparison type of arangement and the results return true or false?

Re: Bool() use?

Posted: Wed Jan 23, 2013 7:28 pm
by Fred
It can be chained in an expression

Re: Bool() use?

Posted: Wed Jan 23, 2013 7:50 pm
by Josh
I absolutely agree with BorisTheOld. Long time ago I had the same discussion on the German forum.

There are some really compiler functions like SizeOf() or OffsetOf(), this functions can be completely replaced at compiletime. On the other hand, we have functions like ClearStructure() and I would be interested to know, how the storage can be cleared at compiletime.