Being dim #1: Array and set enumerators
One of my favourite additions to Delphi over the past years has been the for..in construct and the associated enumerators. I just love the way we can do
var Ch: Char; S: string; begin S := 'Some text'; for Ch in S do // Do something with Ch end;
So how come I missed that enumerators work on sets and arrays? I'm mentioning this here just in case it's slipped past anyone else, and in case you can tell me about any other enumerators I might have missed!
Since forever I've being having to do stuff like this for arrays and sets:
const cMyArray: array[1..4] of Byte = (51, 60, 80, 81); cMySet: set of Byte = [50, 51, 80, 81, 40, 30, 32]; var I: Integer; B: Byte; begin for B := Low(Byte) to High(Byte) do if B in cMySet then // Do stuff with B for I := Low(cMyArray) to High(cMyArray) do // Do stuff my cMyArray[I] end;
When all along I could have been doing:
const cMyArray: array[1..4] of Byte = (51, 60, 80, 81); cMySet: set of Byte = [50, 51, 80, 81, 40, 30, 32]; var Item: Byte; begin for Item in cMySet do // Do stuff with Item for Item in cMyArray do // Do stuff with Item end;
Duh! A refactoring I will go!
Comments
Post a Comment
Comments are very welcome, but please don't comment here if:
1) You have a query about, or a bug report for, one of my programs or libraries. Most of my posts contain a link to the relevant repository where there will be an issue tracker you can use.
2) You have a query about any 3rd party programs I feature, please address them to the developer(s) - there will be a link in the post.
3) You're one of the tiny, tiny minority who are aggressive or abusive - in the bin you go and reported you will be!
Thanks