Lazarus

Free Pascal => General => Topic started by: Nimral on July 30, 2021, 07:56:34 pm

Title: IfThen: "Error: Generics without specialization cannot be used"
Post by: Nimral on July 30, 2021, 07:56:34 pm
Hi,

I get the above message with this code:

Code: Pascal  [Select][+][-]
  1.   Move(s[1],rec[0],IfThen(l > recsize,recsize,l));
  2.  

At first glance, a generic doesn't seem involved. Looking deeper, I found this interesting new section in Sysutils:

Code: Pascal  [Select][+][-]
  1. {$IFNDEF VER3_0}
  2. generic function IfThen<T>(val:boolean;const iftrue:T; const iffalse:T) :T; inline; overload;
  3. {$ENDIF}  
  4.  

Okay, I found a fix quite quickly, "uses math" and write

Code: Pascal  [Select][+][-]
  1.   Move(s[1],rec[0],Math.IfThen(l > recsize,recsize,l));
  2.  

and everything works like before. I do, however, wonder what that Generic in Sysutils is intended to be used for.

FPC 3.2.2/Lazarus 2.2.0RC1

Best regards, Armin.
Title: Re: IfThen: "Error: Generics without specialization cannot be used"
Post by: PascalDragon on August 01, 2021, 12:58:27 pm
I do, however, wonder what that Generic in Sysutils is intended to be used for.

It's intended to be used for those types that are not covered by the one in unit Math:

Code: Pascal  [Select][+][-]
  1. specialize IfThen<String>(SomeBool, 'Foo', 'Bar');
  2. specialize IfThen<TObject>(SomeBool, Instance1, Instance2);

Once the patch for implicit specializations is applied (once the last few problems are worked out) the following will work as well (with modeswitch ImplicitSpecializations enabled):

Code: Pascal  [Select][+][-]
  1. IfThen(SomeBool, 'Foo', 'Bar');
  2. IfThen(SomeBool, Instance1, Instance2);
TinyPortal © 2005-2018