This week I used some record helpers for intrinsic types and I think that they are a wonderful feature. This means that you should update at least to XE3 to use this great feature.
But as always there are some issues: I had an resourcestring and wanted to access to the length.
program TestHelper; {$APPTYPE CONSOLE} uses System.SysUtils; resourcestring SAnswer = '42'; begin Writeln('The length of ' + SAnswer + ' is:'); Writeln(SAnswer.Length); Readln; end.
I tried to compile this code and it doesn’t! The compiler doesn’t know the method length although in System.SysUtils there is the TStringHelper with this method! I had been very surprised but then I thought that a resourcestring is not a string but only assignable to a string, something like a type string.
With XE4 this code does compile which is the expected behavior! Many thanks for this fix.
I talked about this issue to a friend (Many thanks to him!) and he told me that I should try a record helper for a WideString.
program TestHelper; {$APPTYPE CONSOLE} uses System.SysUtils; resourcestring SAnswer = '42'; type TWideStringHelper = record helper for WideString public function Length: Integer; inline; end; { TWideStringHelper } function TWideStringHelper.Length: Integer; begin Result := System.Length(Self); end; begin Writeln('The length of ' + SAnswer + ' is:'); Writeln(SAnswer.Length); Readln; end.
And this code does compile! This means that a resourcestring is a WideString. Again, I’m surprised. But run this small project. It shows that the length of ’42’ is 1. This is a bug!!! My friend told me that a there is already the Quality Central item 112844 for this bug. This means that you shouldn’t use a record helper for resourcestrings in XE3. I hope that this bug will be fixed in XE4, so stay tuned….
Again, under XE4 the resourcestring works like a string. This means that a TWideStringHelper doesn’t work, instead the TStringHelper does. And the length is the correct value. So many thanks for this fix. This is a very good reason to move to XE4.