Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-10

Printing types in generics

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

How should you print types in generics for debugging? here’s an example of how I’m doing it now but it’s hacky (forced type casting pointers) and tkSString crashes like it is. There should be a better solution I think.

procedure TStaticArray.PrintValue (value: T);
begin
	case typeKind of
		tkClass:
			begin
				if not IsDefault(value) then
					TObjectPtr(@value)^.Show
				else
					writeln('nil');
			end;
		tkPointer:
			begin
				if not IsDefault(value) then
					writeln(HexStr(PPointer(@value)^))
				else
					writeln('nil');
			end;
		tkRecord:
			writeln('record');
		tkSString:
			// TODO: crashes.
			writeln(PString(@value)^);
		otherwise
			writeln(PInteger(@value)^); // this is just a hack to print compiler types
	end;
end;

Regards,
Ryan Joseph

Sven Barth
to Ryan Joseph
"Ryan Joseph" wrote
How should you print types in generics for debugging? here’s an example

of how I’m doing it now but it’s hacky (forced type casting pointers) and tkSString crashes like it is. There should be a better solution I think.

There isn't really a better solution, except that you could move that code to a separate class or record so that you can use it in other classes/record than TStaticArray as well...
And your case for tkSString crashes, because that is a ShortString, not a AnsiString thus you need to cast to PShortString (best avoid "(P)String" in this context as this could lead to different results depending on the modeswitches). For the other string types you need to look at tkAString (AnsiString), tkWString (WideString) and tkUString (UnicodeString).

Regards,
Sven

Ryan Joseph
to Sven Barth
On Oct 19, 2017, at 12:58 PM, Sven Barth via fpc-pascal wrote: And your case for tkSString crashes, because that is a ShortString, not a AnsiString thus you need to cast to PShortString (best avoid "(P)String" in this context as this could lead to different results depending on the modeswitches). For the other string types you need to look at tkAString (AnsiString), tkWString (WideString) and tkUString (UnicodeString).

I know why it why can’t type helpers be used here? I though you could add those and just call .Show() or something but I couldn’t figure that out.

Regards,
Ryan Joseph

Sven Barth
"Ryan Joseph" wrote 7 lines
> On Oct 19, 2017, at 12:58 PM, Sven Barth via fpc-pascal < wrote: > > And your case for tkSString crashes, because that is a ShortString, not

a AnsiString thus you need to cast to PShortString (best avoid "(P)String" in this context as this could lead to different results depending on the modeswitches). For the other string types you need to look at tkAString (AnsiString), tkWString (WideString) and tkUString (UnicodeString).

Show quoted text 5 lines
> I know why it 're welcome. :) Btw why can’t type helpers be used here? I though you could add those and

just call .Show() or something but I couldn’t figure that out.

You'd need a type helper for every type that you might specialize with and all those would need to be in scope during the declaration (not specialization!) of your generic as otherwise they wouldn't be found during specialization.

Regards,
Sven