Lazarus
Free Pascal => General => Topic started by: ArminLinder on July 30, 2021, 07:56:34 pm
Title:
IfThen: "Error: Generics without specialization cannot be used"
Post by:
ArminLinder
on
July 30, 2021, 07:56:34 pm
Hi,
I get the above message with this code:
Code: Pascal
[Select]
[+]
[-]
Move
(
s
[
1
]
,
rec
[
0
]
,
IfThen
(
l > recsize
,
recsize
,
l
)
)
;
At first glance, a generic doesn't seem involved. Looking deeper, I found this interesting new section in Sysutils:
Code: Pascal
[Select]
[+]
[-]
{$IFNDEF VER3_0}
generic
function
IfThen<T>
(
val
:
boolean
;
const
iftrue
:
T
;
const
iffalse
:
T
)
:
T
;
inline
;
overload
;
{$ENDIF}
Okay, I found a fix quite quickly, "uses math" and write
Code: Pascal
[Select]
[+]
[-]
Move
(
s
[
1
]
,
rec
[
0
]
,
Math
.
IfThen
(
l > recsize
,
recsize
,
l
)
)
;
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
Quote from: Nimral on July 30, 2021, 07:56:34 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]
[+]
[-]
specialize IfThen<String>
(
SomeBool
,
'Foo'
,
'Bar'
)
;
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]
[+]
[-]
IfThen
(
SomeBool
,
'Foo'
,
'Bar'
)
;
IfThen
(
SomeBool
,
Instance1
,
Instance2
)
;
TinyPortal
© 2005-2018