Recent

Author Topic: When to use WordBool and LongBool?  (Read 6827 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
When to use WordBool and LongBool?
« on: February 18, 2017, 01:43:11 pm »
The size of Boolean also ByteBool is 1 byte only. Why do we need the bigger WordBool and LongBool? What are the use cases of them?

balazsszekely

  • Guest
Re: When to use WordBool and LongBool?
« Reply #1 on: February 18, 2017, 01:49:14 pm »
It is most useful when interfacing with C and C++ application. Under windows I used extensively on winapi calls.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: When to use WordBool and LongBool?
« Reply #2 on: February 18, 2017, 03:18:31 pm »
Yes. It is the relation to other languages, but not only that.
In computer science - you know that - only false is defined enough to be usable, it being zero, empty. True is simply not false and that is undefined, but unequal to zero.
How a language implements that is really... ahum...language dependent. Hence we have multiple options in Pascal to describe boolean values.

To complicate matters further, since 3.0 we can also redefine true/false as e.g. string. Or pointers...
Code: Pascal  [Select][+][-]
  1. const false=nil;
but that gives you a nice "feature" that you can't possibly define true...At least I couldn't write const true = not nil; weekend puzzle or bug.
« Last Edit: February 18, 2017, 03:20:18 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: When to use WordBool and LongBool?
« Reply #3 on: February 18, 2017, 03:54:05 pm »
WinAPI define a BOOL as an int, in this case, for example, 256 is True, but it does not fit into Boolean

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: When to use WordBool and LongBool?
« Reply #4 on: February 18, 2017, 04:28:52 pm »
I do not have language knowledge outside Pascal and Basic and I rarely call WinAPI directly.

Thank you for the information.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: When to use WordBool and LongBool?
« Reply #5 on: February 18, 2017, 04:33:56 pm »
An int holds even 32768 as true... not 256.. even -32767 is still true... Nonsense. Even MS compilers do just !=0 for true. The size of a boolean depends on the underlying library. What to expect is defined in the API documentation.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: When to use WordBool and LongBool?
« Reply #6 on: February 18, 2017, 04:35:39 pm »

WinAPI define a BOOL as an int, in this case, for example, 256 is True, but it does not fit into Boolean
I do not have language knowledge outside Pascal and Basic and I rarely call WinAPI directly.

Thank you for the information.
That should be thank you for "alternative facts"

Note that in pascal it is dependent on the native integer type if a bool is a byte or not.
On most platforms it is a native integer type in pascal. Because the compiler processes that more efficiently.
« Last Edit: February 18, 2017, 04:41:59 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

z505

  • New Member
  • *
  • Posts: 38
  • think first, code after
Re: When to use WordBool and LongBool?
« Reply #7 on: May 12, 2017, 12:45:34 pm »
What is the most compatible (best) boolean to use on unix/macOS/Windows if making a library that many programs and languages will use?

The fact that a boolean can be confusing is cause for concern ;-)

I'm thinking longbool, but not sure.. just a gut hunch which could be wrong.

think first, code after

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: When to use WordBool and LongBool?
« Reply #8 on: May 12, 2017, 12:54:01 pm »
What is the most compatible (best) boolean to use on unix/macOS/Windows if making a library that many programs and languages will use?

The fact that a boolean can be confusing is cause for concern ;-)

I'm thinking longbool, but not sure.. just a gut hunch which could be wrong.

False is easy: that's almost always compatible and resolves to 0.
However, there is no way to guarantee what value represents true. true is not cross-platform on any system or language.
So basically you need to read documentation and use what the system asks for.
Even true := not false isn't true in all cases. True is simply not portable between languages. And that's the truth. :D

Then you have the choice: zero vs One or Zero vs Minus One vs True := not zero : pick one and document it.
« Last Edit: May 12, 2017, 12:57:39 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

z505

  • New Member
  • *
  • Posts: 38
  • think first, code after
Re: When to use WordBool and LongBool?
« Reply #9 on: May 12, 2017, 01:01:51 pm »
so sadly then, maybe just use an integer :-( and then document it

I would prefer to use something more standard like a LongBool that is available on all platforms...

I know at least what could be done for Windows systems: look at what the windows API uses and then use it.

But as for making something standard across unix/windows/macOS?  The only thing I can think of for tips, is maybe to look into OpenGL and see what they use for a boolean? As opengl runs on many systems, not just windows...
think first, code after

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: When to use WordBool and LongBool?
« Reply #10 on: May 12, 2017, 01:30:13 pm »
There is no standard across platforms and languages. There are standards on a per language basis, though.
FPC knows about all of them. And in FPC true and false can be re-defined because they are not keywords....
Code: Pascal  [Select][+][-]
  1. const true = 10;false = 'wrong';
  2. begin
  3.   writeln(false);
  4.   readln;
  5. end.
« Last Edit: May 12, 2017, 01:34:19 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018