[fpc-pascal] Proper preprocessor?

Florian Klämpfl florian at freepascal.org
Fri Jun 22 22:13:47 CEST 2018


Am 22.06.2018 um 22:07 schrieb Sven Barth via fpc-pascal:
> Am 22.06.2018 um 10:12 schrieb Ryan Joseph:
>>
>>> On Jun 22, 2018, at 12:24 PM, Sven Barth via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>>>
>>> If $Assertions is set to Off the complete Assert() line will be absent from the compiled code.
>>
>> Good to know thanks.
>>
>>
>> Here’s an example of something I’ve seen for debugging. I think that was kind of cool you could print types like that 
>> and I’m not sure how that would work in Pascal if at all. Maybe some RTTI magic perhaps.
>>
>> {$define typestr(t):='#t: '+IntToStr(sizeof(#t))}
>>
>> program macro_test;
>> uses
>>     SysUtils;
>>
>> type
>>     MyRecord = record
>>         x, y, z: single;
>>     end;
>>
>> begin
>>     writeln(typestr(MyRecord)); // MyRecord: 12
>> end.
> In trunk that can be done rather nicely:
> 
> === code begin ===
> 
> program ttest;
> 
> {$mode objfpc}
> 
> uses
>    TypInfo;
> 
> type
>    TMyRecord = record
>      x, y, z: Single;
>    end;
> 
> generic function TypeStr<T>: String;
> var
>    ti: PTypeInfo;
> begin
>    ti := PTypeInfo(TypeInfo(T));
>    WriteStr(Result, ti^.Name, ': ', SizeOf(T));
> end;
> 
> begin
>    Writeln(specialize TypeStr<TMyRecord>);
> end.
> 
> === code end ===

Or even

program ttest;

{$mode objfpc}

uses
   TypInfo;

type
   TMyRecord = record
     x, y, z: Single;
   end;

generic function _TypeStr<T>: String;
var
   ti: PTypeInfo;
begin
   ti := PTypeInfo(TypeInfo(T));
   WriteStr(Result, ti^.Name, ': ', SizeOf(T));
end;

{$macro on}

{$define TypeStr:=specialize _TypeStr}

begin
   Writeln(TypeStr<TMyRecord>);
end.


;)




More information about the fpc-pascal mailing list