Re: [fpc-pascal] TypeInfo question
Am 24.03.2017 03:51 schrieb "Ryan Joseph": > 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; > TObjectMatrix = specialize TMatrix; > > > 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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] TypeInfo question
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; TObjectMatrix = specialize TMatrix; function TMatrix.RetainValue (value: T): T; begin if typeKind = tkClass then TObject(value).Retain; result := value; end; Regards, Ryan Joseph ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] TypeInfo question
> 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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] TypeInfo question
Am 23.03.2017 16:29 schrieb "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. TypeInfo() simply inserts the load of a pointer value, so there's not even a call. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal