Friday, November 7, 2008

That which is old is new again

Last week at PDC, I was able to make it to the repeat session of Anders Hejlsberg's talk on the future of C#.  A talk by Anders is always interesting and enjoyable. He showed some really interesting things; but the most intriguing thing was the new "dynamic" keyword. Anders demonstrated several interesting uses for this "newfangled" gizmo, such as interoperating with JavaScript code. Hmm... I think I demonstrated something just like that over a year ago with Delphi. In that demonstration I showed how you could get JavaScript to access Delphi components dynamically. The reverse could just as easily have been done by using a Variant along with the very old (like Delphi 3 old) notion of dynamic variant dispatching. Any variant can be treated like an object in that you can simply start calling any old method or accessing any old property without even having a declaration for what that property is. For instance for many years you've been able to do this:

procedure TForm1.Button1Click(Sender: TObject);
var
Doc, Word: Variant;
begin
Word := CreateOleObject('Word.Application');
Doc := Word.Documents.Add();
Doc.Content.InsertAfter('This is some sample Text');
Word.ActiveWindow.Visible := True;
end;

Just add ComObj to your uses list and you can now paste that code into a button click, and you're automating MS Word. The Delphi Variant type (or the more COM/OLE specific OleVariant) support this dynamically typed, dynamic invocation.


I had a chance to meet with Anders for a few moments out in the "Big Room" and I mentioned that the whole "dynamic" thing looked remarkably like the old Variant dispatching stuff. He chuckled and mentioned that the C# model allows for doing different kinds of dispatching and not just COM IDispatch... Hmm... I smiled and reminded him that Delphi does as well. I explained that since Delphi initially introduced the Variant dispatching in D3, we've added support for custom variant types. We now support all kinds kind of dynamic dispatching by creating your own custom variant type by descending from TInvokableVariantType over in the Variants unit. He paused a moment, chucked again, and explained that really none of these concepts are new, we're just all learning how to reconcile the strongly-type world with the dynamically typed world of JavaScript, Python, and Ruby. Using the above notion of a TInvokeableVariantType, you could create a custom variant type that allows you to directly call and manipulate... oh... say... some RESTful service. It would be interesting to be able to do this:

procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
Twitter, Timeline: Variant;
begin
Twitter := OpenXMLRESTConnection('http://twitter.com/statuses');
Timeline := Twitter.public_timeline();
//for I := VarArrayLowBound(Timeline.statuses, 0) to VarArrayHighBound(Timeline.statuses, 0) do
for I := 0 to Timeline.statuses.count - 1 do // This is far better and easier to use.
begin
Memo1.Lines.Add(Format('User: %s; Message: %s', [Timeline.statuses[I].user.name, Timeline.statuses[I].text]));
Memo1.Lines.Add('');
end;
end;

The above code is purely conjecture on my part, but I can certainly envision such an easy to use, dynamic mechanism to accessing RESTful services.


So, we've appeared to have, yet again, come full circle. That which was old is now new again. Hmm... maybe some of those old codgers back in the 70s, 80s, and 90s were on to something? I just hope we're not merely repeating history, but rather building on it and recognizing that there are only a few new ideas under the sun... knowledge and ideas are not silos unto themselves, but rather built on top of that which has come before. So maybe you could go back even further? Even with the invention of the the first widely used "programable" devices (the programmable fabric loom head) by Jacquard, many of the concepts and ideas of modern computing today can be easily traced back to 1801 and the great pioneers and thinkers of the day.


You can read about the Twitter API here and see how the above code snippet could easily be mapped to it.  To see the XML response just navigate here: http://twitter.com/statuses/public_timeline.xml.