[fpc-devel] [Suggestion] Enumeration range-check intrinsic
J. Gareth Moreton
gareth at moreton-family.com
Fri Jul 5 02:35:29 CEST 2019
Ah, my apologies, Michael.
I can see the issue of it being a convenience thing, but given that many
programmers have fallen foul of the lack of range checking in case
blocks, the typecasting necessary to avoid the problem is cumbersome,
adds a performance penalty and, ultimately, implies you should just not
use enumerations at all in that instance.
When an issue is raised from many different sources, it usually means
something is counterintuitive. We only know about it because we've done
a lot of compiler development.
Gareth aka. Kit
On 05/07/2019 01:17, Michael Van Canneyt wrote:
>
>
> On Fri, 5 Jul 2019, J. Gareth Moreton wrote:
>
>> It's not so much convenience... it's more the fact that the compiler
>> can and will optimise out "if (MyValue>=ord(Low(TMyType))) and
>> (MyValue<=Ord(High(TMyType))) then" because it has every reason to
>> assume that MyValue cannot possibly take on a value that's less than
>> Low(TMyType) or greater than High(TMyType), hence it gets shortcut
>> and assumed to always be True.
>
> No. MyValue is an integer. Hence the ord().
>
> Obviously the check must be done before the typecast, not after...
>
> Let me make that more explicit:
>
> if MyIntValue is TMyEnum then
> MyValue:=TMyEnum(MyIntValue);
> or
> MyValye:=MyIntValue as TMyEnum;
>
> That's why I said it it is not at odds with the fact that the compiler
> can
> assume MyValue is always valid.
>
> Michael.
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
>
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
More information about the fpc-devel
mailing list