A few weeks ago (I've collected many links in my "for blog" bookmarks recently) Danny Thorpe blogged about Dynamic Method Invocation, claiming Delphi did years ago what C# is aiming at now (the history of Delphi leading on the tech side is repeating). His post was a response to the post of yet-another ex-Borlander, Charlie Calvert, who posted about Dynamic Lookups in C# in a Future Focus column of his blog. I find the proposed C# syntax confusing, and don't think that making out the section of code for dynamic invocation at the caller side makes a lot of sense, but I had only a cursory view.
Let me start (today) by showing you how the call looks like:
var
v: variant;
begin
v := VarDSLDateTimeCreate (now);
v.NextMonday;
v.am7;
This sets the date and time represented by the variant to 7 am next Monday. I had a much better invocation mechanism by embedding the variant within an actual object, something I'll work on for a future post. Before that, I'll show you the custom variant architecture. All I want to show you for today, is that NextMonday and 7am are not hard-coded functions. They don't exist. The object receives a call and parses and interprets the function name to determine what to do! Here is a snippet, I'll expand soon with the details:
function TDslDateTimeVariantType.DoProcedure(const V: TVarData;
const Name: string; const Arguments: TVarDataArray): Boolean;
var
tmp: string;
value, month, day: Integer;
tmpDate: TDateTime;
begin
if Pos ('AM', Name) > 0 then
begin
// parse and process...
tmp := StringReplace (Name, 'AM', '', []);
value := StrToIntDef (tmp, 0);
// yes this is ugly!!!
TDslDateTimeVarData(V).VDateTime.DateTime :=
RecodeTime (TDslDateTimeVarData(V).VDateTime.DateTime, value, 0, 0, 0);
end;
...
if Pos ('NEXT', Name) > 0 then
begin
// parse and process...
tmp := StringReplace (Name, 'NEXT', '', []);
if tmp = 'MONDAY' then
begin
// compute next monday date...
...
Stay tuned for the rest of the code and an improved version of it.