Yesterday, while traveling in train to Rome, I've found some time to improve the source code posted earlier for dynamic methods invocation. The code is now close to the point of being reusable and very easy to use. But it is still not complete... here is the description of the idea, I'll post the new complete code at a later time (or possibly put it in some SVN repository) and also get back to discuss why and when you want to write code like this. One thing at a time.
First of all, the code I showed earlier made an explicit use of variants and this is far from great. It also looked very little object oriented. What I've done in this new version is embed the custom variant in a class, so that you access the dynamic features with a special purpose variant property. For example, you can now write:
var
md: TMyDate;
begin
md := TMyDate.Create;
md.v.NextMonday;
In practice with this code you create an instance of a regular class with some data, some methods, plus some dynamic methods. In this case the "v" property is used to access to the dynamic features of the class. Now, I tried to find a better identifier. Stretching Object Pascal rules a bit (what is a character in an identifier?) I came up with the following alternatives (using keys on my keyboard):
md.§.NextMonday;
md.&do.dec24;
md.°.NextMonday;
md.€.NextMonday;
I thought "do" would have been nice but the need for an extra ampersand makes it very hard to pick. Any ideas?
Anyway, the beauty of this approach compared to the previous one is that now the data you are accessing dynamically is plain data with the class (TMyDate). Only, you have to inherit it from a base dynamic variant support class and provide a proper "DoProcedure" to it. In a few minutes you can have a class with a partial dynamic interface... but for the full code you'll have to wait (still needs a couple of checks). This is the TMyDate definition:
type
TMyDate = class (TDynamicClass)
private
fDate: TDateTime;
public
constructor Create;
function DoProcedure(const V: TVarData; const Name: string;
const Arguments: TVarDataArray): Boolean; override;
function AsString: string; override;
end;
It will be that simple to have your dynamic data type... because Delphi rules!