Bool() use?

Just starting out? Need help? Post your questions and find answers here.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Bool() use?

Post 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). ;)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: Bool() use?

Post by Arctic Fox »

Code: Select all

result = Bool(num1 And num2)
:)

The help entry for Bool() is in 'Compiler Functions'.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Bool() use?

Post 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)" ?
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Bool() use?

Post 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.
"Have you tried turning it off and on again ?"
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Bool() use?

Post by MachineCode »

Thanks, Luis. :)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: Bool() use?

Post 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
“Fear is a reaction. Courage is a decision.” - WC
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Bool() use?

Post 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.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Bool() use?

Post 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
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Bool() use?

Post 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? :)
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Bool() use?

Post 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.
BERESHEIT
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Bool() use?

Post 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.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Bool() use?

Post 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
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Bool() use?

Post 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?
Last edited by SFSxOI on Thu Jan 24, 2013 8:57 am, edited 1 time in total.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Bool() use?

Post by Fred »

It can be chained in an expression
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Bool() use?

Post 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.
sorry for my bad english
Post Reply