Duck Duck Delphi

 

DuckDuckDelphi is an Open Source (MIT License) unit for Delphi XE2+ which facilitates duck typing in a normally strongly typed language.

For more information on Duck Typing, please check out this article on Wikipedia.

With Duck Typing, you accomplish tasks by knowing what an object looks like and what it can do, rather than knowing what it is.  For example instead of typing:

if AObject is TEdit then 
  TEdit(AObject).visible := False; 

... you would instead inspect the object to see if it has a visible proprety and act upon it regardless the type...

if AObject.duck.has('visible') then 
  AObject.duck.sett('visible', False);

This alone is interesting, but what if you wanted to operate on all objects that have a visible property?  say for example you wanted to hide all objects on a form...

Form1.duck.all.has('visible').sett(false);

This code iterates through all objects on the form and sets the ones that have a "visible" property to false, regardless their type.

What if you want to clear all memos, text edits, etc on a form via a buttonclick?

procedure TForm1.Button2Click(Sender: TObject);
begin
  duck
.all.can('Clear').go;
end;

pretty simple, eh?

And this obviously is not limited to visual components or objects of a form. Lets say you have an object list that you want to update en-mass...

var list : TList<TObject>
.... create and fill list ...
list
.duck.all.has('timestamp').set(Now);

Duck typing in a strongly typed language such as Delphi has never been easy. Now you can have the best features of a strongly typed language and yet use duck typing whererever it makes sense.

Give DuckDuckDelphi a try and I'm sure you will find yourself using it in nearly every Delphi project very soon.

The project is hosted on BitBucket:

https://bitbucket.org/sivv/duckduckdelphi