Recent

Author Topic: lazarus have if_variant. how to improve for fpc? [SOLVED]  (Read 1537 times)

d2010

  • Full Member
  • ***
  • Posts: 239
lazarus have if_variant. how to improve for fpc? [SOLVED]
« on: November 05, 2024, 03:48:27 am »
Hello  I discovery in Delphi3Old the function if_variant
The function "if_variant" contatin too many bugs in Delphi3.

--a) how to compare s1,s2 with nil value OR how to protect  if_variant with Nil crash
Code: [Select]
Function if_variant(condtion:wordbool;s1,s2:variant):variant;
Begin 
       if (condtion) then if_variant:=s1 else    if_variant:=s2;
End;
« Last Edit: May 06, 2025, 08:25:14 am by d2010 »

Lansdowne

  • New Member
  • *
  • Posts: 39
Re: Lazarus have if_variant. how to improve for fpc?
« Reply #1 on: November 05, 2024, 05:18:13 am »
While requires a Boolean, so surely you don't need to use any Variant there?

As far as I know, if a or b will not try to evaluate b if a is True. So depending on what you're trying to do, would this work?

Replace
 
Code: Pascal  [Select][+][-]
  1. while (if_variant(extmax<>nil,(extmin<extmax)and(extmin^<>atonexit_c),extmin^<>atonexit_c)) do
  2.  

with
 
Code: Pascal  [Select][+][-]
  1. while (extmin^<>atonexit_c) and ((extmax=nil)or(extmin<extmax)) do
  2.  

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: Lazarus have if_variant. how to improve for fpc?
« Reply #2 on: November 05, 2024, 05:20:27 am »
There is generic IfThen in SysUtils.

In your case you can use it like this:
Code: Pascal  [Select][+][-]
  1. while specialize IfThen<Boolean>(extmax <> nil, (extmin < extmax) and (extmin^ <> atonexit_c), extmin^ <> atonexit_c) do begin
  2.   // ...
  3. end;

If you want Variant version, thats possible too:
Code: Pascal  [Select][+][-]
  1. writeln('true  = ', specialize IfThen<Variant>(true,  42, 'hello'));
  2. writeln('false = ', specialize IfThen<Variant>(false, 42, 'hello'));
« Last Edit: November 05, 2024, 05:22:18 am by Fibonacci »

Lansdowne

  • New Member
  • *
  • Posts: 39
Re: Lazarus have if_variant. how to improve for fpc?
« Reply #3 on: November 05, 2024, 08:48:39 am »
Whatever you write in ???? and ??? should include setting result:=.

Beyond that, all I suggest is not to use Variant unless you genuinely need it.

Or if, as a beginner to Pascal, you want to explore how pointers work, do some simple test routines to make sure understand how they work with the usual Types such as String, PChar, Integer (perhaps including ShortInt and Smallint).  And I wouldn't use Cardinal, as far as I know that is rarely used.

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Lazarus have if_variant. how to improve for fpc?
« Reply #4 on: November 05, 2024, 09:02:45 am »
The second part of Fibonacci's last answer is what you want. Try that first. (I wrote the generic ifthen from sysutils)
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

d2010

  • Full Member
  • ***
  • Posts: 239
Re: Lazarus have if_variant. how to improve for fpc?
« Reply #5 on: November 05, 2024, 09:31:19 am »
Whatever you write in ???? and ??? should include setting result:=.
Beyond that, all I suggest is not to use Variant unless you genuinely need it.
Or if, as a beginner to Pascal, you want to explore how pointers work, do some simple test routines to make sure understand how they work with the usual Types such as String, PChar, Integer (perhaps including ShortInt and Smallint).  And I wouldn't use Cardinal, as far as I know that is rarely used.
Thank you guy. God bless you.
Thank you guy. God bless you.
==I need only try except inside/outside for  if_variant source code, and  forced all if_variant/s to
_inline or _assemble
r. Many bugs inside Delphi7 , are cannot force to __inline; even
in Lazarus.fpc.exe.
or otherwise you can
  ifhen (test:boolean; ti,tf:integer):integer;??? inline
  ifthen(test:boolean; Const st,sf:string):string;??? inline
  ifthen (test:boolean; di,df:double):double; ??? inline
Best Regards
« Last Edit: November 05, 2024, 09:39:14 am by d2010 »

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Lazarus have if_variant. how to improve for fpc?
« Reply #6 on: November 05, 2024, 10:00:44 am »
You really need only
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. writeln('true  = ', specialize IfThen<Variant>(true,  42, 'hello'));
  3. writeln('false = ', specialize IfThen<Variant>(false, 42, 'hello'));
or
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. writeln('true  = ', IfThen<Variant>(true,  42, 'hello'));
  3. writeln('false = ', IfThen<Variant>(false, 42, 'hello'));
« Last Edit: November 05, 2024, 10:02:45 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

d2010

  • Full Member
  • ***
  • Posts: 239
Re: Lazarus have if_variant. how to improve for fpc?
« Reply #7 on: November 05, 2024, 06:45:17 pm »
Thank/s :(

d2010

  • Full Member
  • ***
  • Posts: 239
Re: Lazarus have if_variant. how to improve for fpc?
« Reply #8 on: February 15, 2025, 04:10:21 am »
You really need only
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. writeln('true  = ', specialize IfThen<Variant>(true,  42, 'hello'));
  3. writeln('false = ', specialize IfThen<Variant>(false, 42, 'hello'));
or
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. writeln('true  = ', IfThen<Variant>(true,  42, 'hello'));
  3. writeln('false = ', IfThen<Variant>(false, 42, 'hello'));
\

Thank you , that is solution.

 

TinyPortal © 2005-2018