Recent

Author Topic: If ShortInt and Boolean then  (Read 983 times)

Flamore

  • New member
  • *
  • Posts: 6
If ShortInt and Boolean then
« on: October 18, 2020, 12:30:01 pm »
Basically, i'm porting an emulator from C to Pascal. But the main problem is that i cannot find a way to have an if statement where both conditions have to be fulfilled whilist they are diffirent types.
How did i try it:
Code: Pascal  [Select][+][-]
  1. if (ctrMode = 3) and (ctrRunning) then
What it gave me:
Code: Pascal  [Select][+][-]
  1. Error: Operator is not overloaded: "Boolean" and "ShortInt"

howardpc

  • Hero Member
  • *****
  • Posts: 3553
Re: If ShortInt and Boolean then
« Reply #1 on: October 18, 2020, 12:35:33 pm »
Try
Code: Pascal  [Select][+][-]
  1. if (ctrMode = 3) and (ctrRunning = 0) then

PascalDragon

  • Hero Member
  • *****
  • Posts: 2262
  • Compiler Developer
Re: If ShortInt and Boolean then
« Reply #2 on: October 18, 2020, 01:25:51 pm »
It should be

Code: Pascal  [Select][+][-]
  1. if (ctrMode = 3) and (ctrRunning <> 0) then

because in C

Code: C  [Select][+][-]
  1. if (something)

checks whether something is not 0.

Zvoni

  • Sr. Member
  • ****
  • Posts: 410
Re: If ShortInt and Boolean then
« Reply #3 on: October 18, 2020, 02:59:58 pm »
Errrr......?

Code: [Select]
    if (ctrMode = 3) and (ctrRunning.ToBoolean) then
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

PascalDragon

  • Hero Member
  • *****
  • Posts: 2262
  • Compiler Developer
Re: If ShortInt and Boolean then
« Reply #4 on: October 18, 2020, 03:19:11 pm »
Errrr......?

Code: [Select]
    if (ctrMode = 3) and (ctrRunning.ToBoolean) then

While right (though it requires the use of the SysUtils unit) Flamore should also learn why something behaves the way it does. Teach how to fish and all that. ;)

Zvoni

  • Sr. Member
  • ****
  • Posts: 410
Re: If ShortInt and Boolean then
« Reply #5 on: October 18, 2020, 04:54:10 pm »
Errrr......?

Code: [Select]
    if (ctrMode = 3) and (ctrRunning.ToBoolean) then

While right (though it requires the use of the SysUtils unit) Flamore should also learn why something behaves the way it does. Teach how to fish and all that. ;)
True  :D
It took me some time, too, to figure out, that C uses Integers as booleans, respectively they use integers in a boolean expression to check if it's 0.
The same with pointers. It took me a while to figure out, that
Code: C  [Select][+][-]
  1. if (SomePointer) {
  2.  
translates to
Code: Pascal  [Select][+][-]
  1. If Assigned(SomePointer) Then...
  2.  
« Last Edit: October 18, 2020, 04:55:50 pm by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Warfley

  • Sr. Member
  • ****
  • Posts: 313
Re: If ShortInt and Boolean then
« Reply #6 on: October 18, 2020, 05:17:14 pm »
C has not explicit type Boolean, but uses ints for this. In Pascal you have the boolean type, so you need to typecast:
Code: Pascal  [Select][+][-]
  1.     if (ctrMode = 3) and LongBool(ctrRunning) then
That said, you probably just want to make ctrRunning a boolean and put the conversion into the assignment. Also the <> 0 is more explicit and therefore probably preferrable for a better readable code

jamie

  • Hero Member
  • *****
  • Posts: 3664
Re: If ShortInt and Boolean then
« Reply #7 on: October 18, 2020, 06:55:05 pm »
c simply accepts just about anything as a Boolean test when not applying a Boolean to it..

I think this would be a great step for fpc to be able to have auto Boolean parsing during the expression phase.

 you take any type that is 0 or empty and it should return as false

empty strings for example where the compiler would just do the '' or length check for you..

 example
   
   If astring then …

 and of course any type of numerical type.

 as it stands now, you can use a function in the expression and it does not require you to use a operator if its a Boolean return, it knows it should assume that.

 so why can't we do that with other types that are not functions? If there are no operators attached to the expression of an identifier then why can't it assume a Boolean operation if possible.. ?

 I don't know but I sure don't let it bother me when I am eating my brunch in the morning ..

The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 3868
  • My goal: build my own game engine using Lazarus
Re: If ShortInt and Boolean then
« Reply #8 on: October 18, 2020, 07:11:09 pm »
Quote
Pascal is a syntactically-strict language. This means that if you make a mistake, the compiler will stop and inform you of the error.
Source: https://wiki.freepascal.org/Basic_Pascal_Introduction

Pascal is strict and that is the reason I like it. I learned and used BASIC but it's too 'liberal', I made a lot of mistakes so I don't use it anymore. I have no problem to follow rules and let my finger to do more works, using Pascal I make less mistakes.

Code: Pascal  [Select][+][-]
  1. var
  2.   Chairs, Tables: Integer;
  3.  
  4. begin
  5.   Chairs := 2;
  6.   Tables := 4;
  7.   if Chairs and Tables then DoSomething;
  8. end;

If Pascal allows Boolean test on numerical type, the 'and' in the code above is a logical operator or bitwise operator?
« Last Edit: October 18, 2020, 08:45:35 pm by Handoko »

Warfley

  • Sr. Member
  • ****
  • Posts: 313
Re: If ShortInt and Boolean then
« Reply #9 on: October 18, 2020, 08:31:14 pm »
c simply accepts just about anything as a Boolean test when not applying a Boolean to it..

I think this would be a great step for fpc to be able to have auto Boolean parsing during the expression phase.
C does not accept anything as boolean, in C there is no such thing as boolean, it is just int. Anything that can implicetly be converted to int does the job, this includes pointer and floats.

About that boolean parsing, well... it exists kinda thanks to operator overloading, take this simple class I've wrote:
Code: Pascal  [Select][+][-]
  1.   TOptional<T> = record
  2.   [...]
  3.   public
  4.     function Get: T; {$IFDEF INLINING}inline;{$ENDIF}
  5.     [...]
  6.     class operator Implicit(constref opt: TOptional<T>): Boolean; {$IFDEF INLINING}inline;{$ENDIF}
  7.     class operator Explicit(constref opt: TOptional<T>): Boolean; {$IFDEF INLINING}inline;{$ENDIF}
  8.     class operator LogicalNot(constref opt: TOptional<T>): Boolean; {$IFDEF INLINING}inline;{$ENDIF}
  9.     [...]
  10.   end;
  11.  
Thanks to the operator Implicit to boolean one can simply write such code:
Code: Pascal  [Select][+][-]
  1. if someOptional then
  2.   someOptional.Get.DoSomething;
And with the operator not the following is possible:
Code: Pascal  [Select][+][-]
  1. if not someOptional then
  2.   raise EObjectNotFoundException.Create(...);

While being a little bit cumbersome (especially with the not operator) this works pretty fine. In C++ there is the operator bool that can directly convert a value to bool and therefore enables all the logical operators like not, and, etc, which is not so easiely possible in pascal because C++ handles bools a little bit seperately (while in pascal you would need to overload every combination of and- or and xor with all types you want to couple that with, I for my part therefore usually only overload the implicit, explicit and not and ignore the other operators)

jamie

  • Hero Member
  • *****
  • Posts: 3664
Re: If ShortInt and Boolean then
« Reply #10 on: October 18, 2020, 09:14:40 pm »
I knew the so called experts would just jump out of the wood work and show their wits.


its true C does not have a intrinsic type nor does it need one..

C performs Boolean operations by simple testing the item for a 0 content and that is the same as saying false;
 
 so simply speaking without operators along with "int" for example in a expression statement it performs a Boolean operation.

 you don't need intrinsic bools and for the record, as much as I like pascal structure, I very much like the idea of doing an Boolean operation on identities without all the extra operator crap..

 so you can have this

 if   someinteger then...
 
 or you can have this
if someinteger <> 0 then...

and where pascal would shine even more would be the fact of doing null test for strings,pinters etc.

 if apointer then...

 instead of
 if assigned(apointer)
if Apointer <> nil
etc

 no one should need to write class operators to do this simple task

The only true wisdom is knowing you know nothing

zamronypj

  • Full Member
  • ***
  • Posts: 101
    • Fano Framework, Free Pascal web application framework
Re: If ShortInt and Boolean then
« Reply #11 on: October 18, 2020, 10:45:03 pm »
while

if someint then

looks natural to C programmer. it does not express its intent clearly without background understanding that in C any integer value that are not zero is considered true

if someint<>0 then

is more verbose and expressive and tells you exactly that you want comparison result to be true if someint is not zero.
« Last Edit: October 18, 2020, 10:51:39 pm by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Personal Projects https://v3.juhara.com
Github https://github.com/zamronypj

PascalDragon

  • Hero Member
  • *****
  • Posts: 2262
  • Compiler Developer
Re: If ShortInt and Boolean then
« Reply #12 on: October 19, 2020, 09:31:24 am »
so why can't we do that with other types that are not functions? If there are no operators attached to the expression of an identifier then why can't it assume a Boolean operation if possible.. ?

Because Pascal has strict typing. Operator overloads can muddle this, but by itself Pascal is strict and this will not change.

Zvoni

  • Sr. Member
  • ****
  • Posts: 410
Re: If ShortInt and Boolean then
« Reply #13 on: October 19, 2020, 12:01:39 pm »
Because Pascal has strict typing. Operator overloads can muddle this, but by itself Pascal is strict and this will not change.
Thank you, thank you, thank you.
Yes, it's driving me bonkers to figure out C-Code-Integers being a "real" boolean or a "real" Integer, but i wouldn't have it any other way from the Pascal-side
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Leledumbo

  • Hero Member
  • *****
  • Posts: 8273
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: If ShortInt and Boolean then
« Reply #14 on: October 19, 2020, 01:34:04 pm »
Because Pascal has strict typing. Operator overloads can muddle this, but by itself Pascal is strict and this will not change.
Please keep it that way. Don't let anyone like jamie ever touch the language and the compiler, as once given, it's a step forward towards having truthy and falsy values and rules (hello, PHP! Python! JavaScript! And everyone else with this stupidity!) that make boolean evaluation complex to implement, understand and memorize.

 

TinyPortal © 2005-2018