views:

195

answers:

2

At the moment I give delphi2010 a trial and found the TValue type of the Rtti Unit. TValue have very interessting features, but I can't find a way to assign an interface.

I try the following

program Project1;
uses
  Classes, SysUtils, Rtti;
var
   list : IInterfaceList;
   value : TValue;
begin
  // all these assignments works
  value := 1;
  value := 'Hello';
  value := TObject.Create;

  // but nothing of these assignments works
  list := TInterfaceList.Create;
  value := list; // [DCC Fehler] Project1.dpr(15): E2010 incompatible types: 'TValue' and 'IInterfaceList'
  value.From[list]; // [DCC Fehler] Project1.dpr(16): E2531 Method 'From' requires explicit typarguments
  value.From<IInterfaceList>[list]; // [DCC Fehler] Project1.dpr(17): E2035 Not enough parameters
end.

I can't find any further information. Not in the delphi help system and not on the internet. What do I do wrong?

+4  A: 

Your last try is the closest. TValue.From is a class function that creates a TValue from a parameter. You probably put the square brackets in there because that's how CodeInsight showed it, right? That's actually a glitch in CodeInsight; it does that for generics-based functions, where you should be using parenthesis instead. The proper syntax looks like this:

Value := TValue.From<IInterfaceList>(list);
Mason Wheeler
It's worth pointing out that for simple well-typed argument lists like this, type inference should work fine, and `TValue.From(list)` should be sufficient.
Barry Kelly
Thanks Mason and Barry, I have tried for 2 hours to find a solution. It is hard to realize that Codeinsight was only kidding me.
Heinz Z.
+5  A: 

This is a working version of the program:

program Project1;
uses
  Classes, SysUtils, Rtti;
var
   list : IInterfaceList;
   value : TValue;
begin
  // all these assignments works
  value := 1;
  value := 'Hello';
  value := TObject.Create;

  // but nothing of these assignments works
  list := TInterfaceList.Create;
  value := TValue.From(list);
end.
Barry Kelly