Jump to content
Sign in to follow this  
Mike Torrettinni

Any reservations on using const array[type] of?

Recommended Posts

I wasn't sure how to give better title...

 

I didn't know about array[TInfoType] of construct, until this morning.

 

So, this is how I usually use enum types and assign string names:

 

type
  TInfoType = (itProject, itContacts, itWorker, itWorkers);

const
  cProjectNodeName:  string = 'project';
  cContactsNodeName: string = 'contacts_info';
  cWorkerNodeName:   string = 'WRK';
  cWorkersNodeName:  string = 'WRKS';

...

function GetInfoTypeNodeName(const aInfoType: TInfoType): string;
begin
  case aInfoType of
    itProject:  Result := cProjectNodeName;
    itContacts: Result := cContactsNodeName ;
    itWorker:   Result := cWorkerNodeName;
    itWorkers:  Result := cWorkersNodeName;
  end;
end;

 

And I just noticed I can actually use const array[TInfoType] like this:

 

type
  TInfoType = (itProject, itContacts, itWorker, itWorkers);

const
  cInfoTypeNodeNames: array[TInfoType] of string = ('project', 'contacts_info', 'WRK', 'WRKS');

...
function GetInfoTypeNodeName2(const aInfoType: TInfoType): string;
begin
  Result := cInfoTypeNodeNames[aInfoType];
end;

 

And this looks so much better, and the compiler complains when strings don't match the number of items in TInfoType. Great!

 

But I can't find documentation of array[type] of  construct and I'm worried that I'm getting too excited and will find out of limitation just when I change everything to this new const type.

 

I know it's hard for anybody to guess how this will be used in my code, but in general are there any major limitations of array[type] of construct that I should be aware of? Anybody has any gotchas to point out?

 

Thanks!

Edited by Mike Torrettinni
Edit title.

Share this post


Link to post

The docs say about static arrays:

Quote

Static array types are denoted by constructions of the form:


array[indexType1, ..., indexTypen] of baseType;

where each indexType is an ordinal type whose range does not exceed 2GB.

So any ordinal type (which includes enumerations) are valid as array index. That has been this way since the invention of Pascal.

  • Like 1
  • Thanks 1

Share this post


Link to post

Thanks, I know about array [1..100] of type but didn't connect this two together.

So, in my case where I have 4 items in type TInfoType:

cInfoTypeNodeNames: array[TInfoType] of string = ('project', 'contacts_info', 'WRK', 'WRKS');

actually means/translates into:

cInfoTypeNodeNames: array[0..3] of string = ('project', 'contacts_info', 'WRK', 'WRKS');

Correct?

Share this post


Link to post

I think that the syntax  was always there and I use it a lot. I found it more useful especially it provides a compiler check about the indexing type.

This code :

NodeNames: array [TInfoType] of string = ('itProject', 'itContacts', 'itWorker', 'itWorkers');

Is exactly equivalent to this one :

NodeNames2: array [Ord(Low(TInfoType)) .. Ord(High(TInfoType))] of string = ('itProject', 'itContacts', 'itWorker', 'itWorkers');

Note that you can also use range:

NodeNames3: array [itContacts .. itWorkers] of string = ('itContacts', 'itWorker','itWorkers');

 

  • Like 1
  • Thanks 2

Share this post


Link to post
46 minutes ago, Mike Torrettinni said:

Correct?

Not quite. You cannot use an integer index for such an array, only the declared index type.

Share this post


Link to post
1 minute ago, Uwe Raabe said:

Not quite. You cannot use an integer index for such an array, only the declared index type.

Hm, probably we are not referring to the same thing... this is what I meant and it compiles OK:

 

cInfoTypeNodeNames2: array[0..3] of string = ('project', 'contacts_info', 'WRK', 'WRKS');

What are you referring to?

Share this post


Link to post
Guest

No reservations, I find it concise/elegant and prefer to use it whenever I can. My 2c.

You might have to avoid enum types with explicitly assigned ordinal values, though.

Share this post


Link to post
19 minutes ago, Ondrej Kelle said:

No reservations, I find it concise/elegant and prefer to use it whenever I can. My 2c.

You might have to avoid enum types with explicitly assigned ordinal values, though.

Thanks! Yes, I never assign values to enums - well, at least I didn't have the need, yet, so all good.

 

Well, this all started when I wanted to have enum type connected with string value... I wanted simple solution, even though I saw some complicated class and record implementations... I need something I understand and can use right away.

And this combination of type and const array[type] is awesome, because I can keep the definitions together in the code, and it also handles a bit of checking - it complains when enum items number and string numbers are not the same, so I can't forget to define one.

Share this post


Link to post
46 minutes ago, Mike Torrettinni said:

this combination of type and cons

Yes and best readability.

 

That said I've moved all to Attributes now:

[EnumNames('TestEnumAttribute', 'Hello, World')]
TTestEnumeration = (First, Second);

and to retrieve it:

Enum<TTestEnumeration>.EnumName('TestEnumAttribute', TTestEnumeration.First) --> 'Hello'

Usually I document where I find this stuff but that seems to have gone missing a quick search didn't bring up any of that but this instead:

https://gist.github.com/ortuagustin/6342894207caa0f71a5d23475670281d

 

It sure saves writing a matrix for each Enum you need in human readable form.

 

 

Share this post


Link to post
5 hours ago, Mike Torrettinni said:

Hm, probably we are not referring to the same thing... this is what I meant and it compiles OK:

 


cInfoTypeNodeNames2: array[0..3] of string = ('project', 'contacts_info', 'WRK', 'WRKS');

What are you referring to?

type
  TInfoType = (itProject, itContacts, itWorker, itWorkers);
  
const
  cInfoTypeNodeNames: array[TInfoType] of string = ('project', 'contacts_info', 'WRK', 'WRKS');
  
begin
  Writeln(cInfoTypeNodeNames[1]); // will not compile
  Writeln(cInfoTypeNodeNames[itContacts]); // will compile
end.
  

 
  • Like 1
  • Thanks 1

Share this post


Link to post
12 hours ago, Uwe Raabe said:

type
  TInfoType = (itProject, itContacts, itWorker, itWorkers);
  
const
  cInfoTypeNodeNames: array[TInfoType] of string = ('project', 'contacts_info', 'WRK', 'WRKS');
  
begin
  Writeln(cInfoTypeNodeNames[1]); // will not compile
  Writeln(cInfoTypeNodeNames[itContacts]); // will compile
end.
  


 

Thanks, I understand now! Good to know.

 

Share this post


Link to post
21 hours ago, Mahdi Safsafi said:

I think that the syntax  was always there and I use it a lot. I found it more useful especially it provides a compiler check about the indexing type.

This code :


NodeNames: array [TInfoType] of string = ('itProject', 'itContacts', 'itWorker', 'itWorkers');

Is exactly equivalent to this one :


NodeNames2: array [Ord(Low(TInfoType)) .. Ord(High(TInfoType))] of string = ('itProject', 'itContacts', 'itWorker', 'itWorkers');

Note that you can also use range:


NodeNames3: array [itContacts .. itWorkers] of string = ('itContacts', 'itWorker','itWorkers');

 

Your explanations really does help people to understand this construct!

Share this post


Link to post
3 hours ago, edwinyzh said:

Your explanations really does help people to understand this construct!

Glad to see that my explanation was helpful. 

Share this post


Link to post
On 2/22/2019 at 5:59 PM, Mike Torrettinni said:

Thanks, I know about array [1..100] of type but didn't connect this two together.

So, in my case where I have 4 items in type TInfoType:


cInfoTypeNodeNames: array[TInfoType] of string = ('project', 'contacts_info', 'WRK', 'WRKS');

actually means/translates into:


cInfoTypeNodeNames: array[0..3] of string = ('project', 'contacts_info', 'WRK', 'WRKS');

Correct?

Incorrect. It is the same as:

cInfoTypeNodeNames: array[itProject..itWorkers] of string = ('project', 'contacts_info', 'WRK', 'WRKS');

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×