[fpc-devel] Dangerous optimization in CASE..OF

Ondrej Pokorny lazarus at kluug.net
Sat Apr 14 10:08:41 CEST 2018

On 14.04.2018 9:59, Michael Van Canneyt wrote:
> On Sat, 14 Apr 2018, Ondrej Pokorny wrote:
>> On 02.07.2017 18:49, Jonas Maebe wrote:
>>> I would be in favour of a new intrinsic.
>> I have to admit that for some usages I would prefer a compiler 
>> intrinsic that returns False instead of raising an exception. 
>> Something like:
>> function TryIntToEnum<T: type of enum>(const AValue: Integer; var 
>> AEnumValue: T): Boolean;
> Please, don't use this ridiculous generics syntax. If it is a compiler 
> intrinsic, then
> function TryIntToEnum(T: atype; const AValue: Integer; var AEnumValue: 
> aType): Boolean;
> will work, just like typeinfo() or sizeof() works.

The syntax I used was just for illustration. You don't type the <T: type 
of enum> part in your own code.

What I wanted to tell was that you can omit the enum type parameter (the 
first parameter "T: atype" from your function). The type should be 
possible to get directly from AEnumValue: aType:

function TryIntToEnum(const AValue: Integer; var AEnumValue: aType): 
Boolean; // aType being an enum type
- If you like this description more :)

Effectively, you should be able to use:
   E: TMyEnum;
   if TryIntToEnum(1, E) then

instead of
   if TryIntToEnum(TMyEnum, 1, E) then


More information about the fpc-devel mailing list