Recent

Author Topic: IfThen: "Error: Generics without specialization cannot be used"  (Read 1485 times)

Nimral

  • Full Member
  • ***
  • Posts: 186
  • Keep it simple.
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.
« Last Edit: July 30, 2021, 07:59:23 pm by Nimral »
Lazarus 2.0.12 & 2.3.0  (trunk) on Windows 10, Raspberry Pi OS "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15

PascalDragon

  • Hero Member
  • *****
  • Posts: 3319
  • Compiler Developer
Re: IfThen: "Error: Generics without specialization cannot be used"
« Reply #1 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