View Issue Details

IDProjectCategoryView StatusLast Update
0036294FPCPackagespublic2019-11-25 22:16
ReporterThaddy de KoningAssigned ToSven Barth 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version3.3.1Product Build43449 
Target VersionFixed in Version3.3.1 
Summary0036294: Request for RTTT TValue: implement TValue.IsType<T>
DescriptionWhile experimenting with some Delphi RTTI examples I had to write a typehelper to get the following code working:
http://robstechcorner.blogspot.com/2009/09/introduction-to-tvalue.html
Steps To ReproduceTry this example from robstechcorner, it does not compile:
program Project10;
{$IFDEF FPC}{$MODE DELPHI}
{$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}

uses
  Rtti,TypInfo;

var
 v : TValue;
 i : Integer;

begin
  i := 10;
  v := I;
  Writeln('V.Kind =',GetEnumName(TypeInfo(TTypeKind),ord(v.Kind)));
  Writeln('V.IsType<Integer> = ',v.IsType<Integer>);
  Writeln('V.IsType<TObject> = ',v.IsType<TObject>);
  Writeln('V.IsObject = ',v.IsObject);
  readln;
end.
Additional Informationtemporary solved it like this:
program Project10;
{$mode delphi}

uses
  Rtti,TypInfo;
type
  TValueHelper = record helper for TValue
  function IsType<T>:Boolean;
  end;
  
  function TValueHelper.IsType<T>:Boolean;
  begin
    Result := self.Kind = PTypeInfo(system.TypeInfo(T)).Kind;
  end;

var
 v : TValue;
 i : Integer;
begin
  i := 10;
  v := I;
  Writeln('V.Kind =',GetEnumName(TypeInfo(TTypeKind),ord(v.Kind)));
  Writeln('V.IsType<Integer> = ',v.IsType<Integer>);
  Writeln('V.IsType<TObject> = ',v.IsType<TObject>);
  Writeln('V.IsObject = ',v.IsObject);
  readln;
end.
TagsNo tags attached.
Fixed in Revision43591
FPCOldBugId
FPCTarget-
Attached Files

Activities

Thaddy de Koning

2019-11-12 08:21

reporter   ~0119237

There are another two missing in this category: I can prepare a patch for all three if the provided code is on the right track

Sven Barth

2019-11-13 17:03

manager   ~0119271

Checking for Kind is wrong: IsType<Double> would return true for a TValue of a Single. You need to check the PTypeInfo itself.

And what do you mean with "another two missing"?

Thaddy de Koning

2019-11-14 09:30

reporter   ~0119289

1. Ah, I see.
2. Delphi supports a few more TValue generic methods that I discovered by testing Rob's example code.

This has no priority, I guess you have these already in your private repository?
I am just exploring.

Sven Barth

2019-11-15 10:45

manager   ~0119309

No, I have not as for now I had no use for these, thus they are low priority for me. If you implement them cleanly with tests (extend the tests in packages/rtl-objpas/tests/tests.rtti.pas) then I'll apply it.

Michael Van Canneyt

2019-11-23 10:00

administrator   ~0119441

@Sven,

I tried to implement this (with test)

But this request exposes a bug in the compiler IMO.

Add this to the TValue:

generic function IsType<T>() : boolean; overload;

generic function TValue.IsType<T>() : boolean;

begin
  Result:=IsType(PTypeInfo(System.TypeInfo(T)));
end;

Then

  checkTrue(v.isType<Integer>,'Integer');

results in the following compiler error:

tests.rtti.pas(1907,30) Error: Illegal expression

On the off chance that it was the implementation that caused an error when specializing, I tried also with an empty implementation.
The result is the same, so I'm pretty sure the compiler has problems finding the correct overload or somesuch.

Sven Barth

2019-11-25 22:15

manager   ~0119494

Last edited: 2019-11-25 22:16

View 2 revisions

I was not able to reproduce /your/ bug, but I managed to get /a/ bug. If you can provide an example that shows the problem, please open a new bug report.

Meanwhile I've implemented TValue.IsType<>.

@Thaddy: If you have other functions that need to be implemented, please open a new report for them.

Please test and close if okay.

Issue History

Date Modified Username Field Change
2019-11-11 14:56 Thaddy de Koning New Issue
2019-11-12 08:21 Thaddy de Koning Note Added: 0119237
2019-11-13 17:03 Sven Barth Note Added: 0119271
2019-11-14 09:30 Thaddy de Koning Note Added: 0119289
2019-11-15 10:45 Sven Barth Note Added: 0119309
2019-11-23 10:00 Michael Van Canneyt Note Added: 0119441
2019-11-25 22:15 Sven Barth Assigned To => Sven Barth
2019-11-25 22:15 Sven Barth Status new => resolved
2019-11-25 22:15 Sven Barth Resolution open => fixed
2019-11-25 22:15 Sven Barth Fixed in Version => 3.3.1
2019-11-25 22:15 Sven Barth Fixed in Revision => 43591
2019-11-25 22:15 Sven Barth FPCTarget => -
2019-11-25 22:15 Sven Barth Note Added: 0119494
2019-11-25 22:16 Sven Barth Note Edited: 0119494 View Revisions