Sign in Join

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

Mailing lists / fpc-pascal / 2017-03

TypeInfo question

5 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

I have some generics which operate on multiple types and instead of making subclasses for certain types and overriding methods I wonder if using the RTTI like:

PTypeInfo(TypeInfo(T))^.kind = tkClass

would be a good idea. How is TypeInfo() implemented exactly? I’m curious if there are any string comparing, allocating memory etc... that would adversely affect performance since these classes need to perform fast data lookups/insertions.

Thanks.

Regards,
Ryan Joseph

Sven Barth
to Ryan Joseph
"Ryan Joseph" wrote
I have some generics which operate on multiple types and instead of

making subclasses for certain types and overriding methods I wonder if using the RTTI like:

PTypeInfo(TypeInfo(T))^.kind = tkClass would be a good idea. How is TypeInfo() implemented exactly? I’m curious

if there are any string comparing, allocating memory etc... that would adversely affect performance since these classes need to perform fast data lookups/insertions.

TypeInfo() simply inserts the load of a pointer value, so there's not even a call.

Regards,
Sven

Ryan Joseph
to Sven Barth
Show quoted text 5 lines
On Mar 23, 2017, at 11:16 PM, Sven Barth via fpc-pascal wrote: TypeInfo() simply inserts the load of a pointer value, so there's not even a call.

Great, that’s what I was hoping. Thanks.

Regards,
Ryan Joseph

Ryan Joseph
to Ryan Joseph

I think I already learned this and I forget when I had the idea about TypeInfo. :)

This is basically the pattern I’m attempting. I have a generic class and I thought I could avoid specializing and making a subclass so that I could call a method if the type of the generic was a class. So what happens as you now is that I can’t cast “value” to TObject because if the generic was specialized as an integer for example, the type cast would be illegal and I get stuck at compile time (Illegal type conversion: "LongInt" to "TObject”).

Is there anyway I could push the type checking to runtime? I wanted to using writeln also to perform some printing for debugging but I get stuck at compile time again trying to mix, integers, strings, records, classes etc… in the generic. I would use an interface but there are compiler types and records involved so that’s not an option.

type
	TLongIntMatrix = specialize TMatrix<LongInt>;
	TObjectMatrix = specialize TMatrix<TObject>;
function TMatrix.RetainValue (value: T): T;
begin
	if typeKind = tkClass then
		TObject(value).Retain;
	result := value;
end;

Regards,
Ryan Joseph

Sven Barth
to Ryan Joseph
"Ryan Joseph" wrote
Is there anyway I could push the type checking to runtime? I wanted to

using writeln also to perform some printing for debugging but I get stuck at compile time again trying to mix, integers, strings, records, classes etc… in the generic. I would use an interface but there are compiler types and records involved so that’s not an option.

Show quoted text 14 lines
type TLongIntMatrix = specialize TMatrix<LongInt>; TObjectMatrix = specialize TMatrix<TObject>; function TMatrix.RetainValue (value: T): T; begin if typeKind = tkClass then TObject(value).Retain; result := value; end;

You could try to do it with a pointer:

PObject(@value)^.Retain;

Regards,
Sven