Recent

Author Topic: Lazarus have if_variant. how to improve for fpc?  (Read 721 times)

d2010

  • Jr. Member
  • **
  • Posts: 65
Lazarus have if_variant. how to improve for fpc?
« 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;

--b) If_variant inside While, if_variant is too slow. Can I extend if_variant with _inline or _assembler? How do ? Is really  more  fast of speed with if_variant+inline.
Code: [Select]
     while (if_variant(extmax<>nil,(extmin<extmax)and(extmin^<>atonexit_c),extmin^<>atonexit_c)) do
       Begin
             fox:=0;
             c1:=extmin^;
             inc(extmin);
             if (c1=#0) then exit;             
             if (c3=atonexit_c) then exit;
             while (not (c1 in ['A'..'z'])) do
               Begin c1:=extmin^;
                     inc(extmin);
                     if (c1=#0) then exit;             
                     if (c3=atonexit_c) then exit;
               End;       
--c) Which if_variant is more good?
Code: [Select]
Function if_variant1(condtion:boolean;s1,s2:variant):variant;
Begin 
       if (condtion) then if_variant:=s1 else    if_variant:=s2;
End;

Function if_variant2(condtion:wordbool;s1,s2:variant):variant;
Begin 
       if (condtion) then if_variant:=s1 else    if_variant:=s2;
End;
« Last Edit: November 05, 2024, 03:57:03 am by d2010 »

Lansdowne

  • New Member
  • *
  • Posts: 35
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: 613
  • 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: 35
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: 16199
  • Censorship about opinions does not belong here.
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)
If I smell bad code it usually is bad code and that includes my own code.

d2010

  • Jr. Member
  • **
  • Posts: 65
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: 16199
  • Censorship about opinions does not belong here.
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 »
If I smell bad code it usually is bad code and that includes my own code.

d2010

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

 

TinyPortal © 2005-2018