Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-03

Associate initializer value with array item type

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

cobines

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

Reply by email

Jeppe Johansen
to cobines

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 :)

cobines wrote 38 lines
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 - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email