[fpc-pascal] Record helpers on built in types?

Sven Barth pascaldragon at googlemail.com
Wed Feb 18 07:52:48 CET 2015


On 17.02.2015 23:28, Anthony Walter wrote:
> I tried this out today on FPC fixes_3_0 and was surprised to see it worked:
>
> type
>    StringHelper = record helper for string
>    private
>      function GetLength: Integer;
>    procedure SetLength(Value: Integer);
>      function GetIsEmpty: Boolean;
>      function GetIsWhitespace: Boolean;
>    public
>      function BeginsWith(const S: string): Boolean;
>      function EndsWith(const S: string): Boolean;
>      function Trim: string;
>      property Length: Integer read GetLength write SetLength;
>      property IsEmpty: Boolean read GetIsEmpty;
>      property IsWhitespace: Boolean read GetIsWhitespace;
>    end;
>
> I love this and would like to know if this is going to be an officially
> supported feature and can I depend on it to work going forward?

http://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_type_helpers

Please note that if you use "String" the types the helper will apply to 
will depend on whatever "String" currently is. Maybe it would be better 
to explicitely use "AnsiString" and "UnicodeString" to avoid potential 
surprises.
And just in case: type helpers might also be used on constants:

=== code begin ===

Writeln('Test'.Length);

=== code end ===

Regards,
Sven




More information about the fpc-pascal mailing list