[fpc-pascal] Associate initializer value with array item type

Jeppe Johansen jepjoh2 at es.aau.dk
Fri Mar 25 00:13:00 CET 2011


I can see why it would be interesting. Simple cases such as lookup 
tables based on booleans for example

In many cases I've needed something like this

function test(a: boolean): longint;
const
   MyLUT: array[boolean] of longint = (0,213); // Assuming false = index 0
begin
    result := MyLUT[a];
end;

Where I simply couldn't find no definite documentation, whether I could 
always rely on that. I could find sources in the compiler/rtl/packages 
relying on that behaviour so I guess the specific case won't a problem :)

Den 24-03-2011 23:43, cobines skrev:
> Hello.
>
> Let's say I have a type:
>
> TClothingType = (ctJacket, ctPants, ctShirt);
>
> and I want to associate it with color:
>
> var
>    Colors: array[TClothingType] = (clRed, clBlue, clGreen);
>
> Is it possible to protect Colors against a change in order of items in
> TClothingType? Adding or removing items from TClothingType will
> generate error that too much/not enough initializers are present. But
> if someone changes the order of items or changes a clothing type to
> different I will get wrong association of colors.
>
> I'm thinking of syntax like this:
>
> var
>    Colors: array[TClothingType] = (ctJacket:clRed, ctPants:clBlue,
> ctShirt:clGreen);
>
> Is something like this possible?
>
> I know I can assign it at runtime:
>
> Colors[ctJacket] := clRed;
> Colors[ctPants] := clBlue;
> Colors[ctShirt] := clGreen;
>
> But I'm looking for compile time method.
>
> --
> cobines
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal




More information about the fpc-pascal mailing list