Recent

Author Topic: I wish for a better BOOLEAN operation within the IF statement.  (Read 9827 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5959
  • Compiler Developer
Re: I wish for a better BOOLEAN operation within the IF statement.
« Reply #45 on: April 04, 2019, 09:26:31 am »
operator implicit is not available for stand-alone operators as in mode objfpc, afaik. Just := and that one is here also a compiler intrinsic. I haven't tested yet if maybe longbool is available.
[edit] nope..
The implicit operator is declared by the := operator overload. The explicit one uses Explicit like in Delphi.

Thaddy

  • Hero Member
  • *****
  • Posts: 16928
  • Ceterum censeo Trump esse delendam
Re: I wish for a better BOOLEAN operation within the IF statement.
« Reply #46 on: April 04, 2019, 09:47:37 am »
Yes, but that is also taken by the compiler, hence the hardcast already works.
And as such these four all fail with impossible operator overload:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. operator explicit(const value:integer):Boolean;
  3. begin
  4.   Result := value <> 0;
  5. end;
  6. operator :=(const value:integer):Boolean;
  7. begin
  8.   Result := value <> 0;
  9. end;
  10. operator explicit(const value:Longint):LongBool;
  11. begin
  12.   Result := value <> 0;
  13. end;
  14. operator :=(const value:longint):Longbool;
  15. begin
  16.   Result := value <> 0;
  17. end;
Which is rather a pity, since assignment from integer to boolean doesn't work without a cast, so implicit seems not to work at all. Is this a bug, Sven?
Especially longbool implicit assignment from a longint has scenarios that are useful, like preventing execution of a division if a value evaluates to false.
« Last Edit: April 04, 2019, 10:13:43 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018