Continuing my look at Delphi Prism, in my previous post I took a (very short) look at some of the compatibility changes required to port my Delphi.NET Twitter Library to Delphi Prism, I mentioned that I now needed to learn about some of the new language features provided by Prism.
Ever since the release of Delphi.NET there has been much confusion surrounding why you would choose the Delphi language for .NET Development. When Delphi.NET was released there was concern that it was not a first class member of the .NET framework and always seemed to lag 12-18 months behind the lightning fast pace of development of the .NET Framework. With Prism Delphi offerings for .NET are based upon the RemObjects Oxygene compiler which isn’t required to maintain a loose language compatibility with the Win 32 Delphi compiler and therefore is able to progress at a much faster pace.
Still, despite this: A recent Delphi Prism related thread at StackOverflow showed that there still seems to be a general feeling that Delphi is a second class citizen in the .NET Development World. Some even seemed to suggest that the language features in Prism make it unrecognisable as Delphi. To try to dispense with this idea: I thought that I would show off some of my own favourite new language features in Delphi Prism and ones which for me make it a natural fit for .NET.
Please note: This is not intended to be a comprehensive guide to the new language features. For that you’ll need to refer to the Delphi Prism Wiki page on Compatibility with Win 32 Delphi.
My favourite new language features which Prism brings to the table for Delphi Developers is LINQ. Olaf Monien, who I also follow on Twitter (@omonien), posted a beautifully simple example of using LINQ with Delphi Prism on his own blog and then showed how Delphi Prism can also do LINQ to SQL. I have replicated his basic example below because it also demonstrates another great language feature.
// Type Declaration
MusicAlbum = record
public
Name: string;
Artist: string;
AlbumYear: integer;
end;
var
MyAlbum: MusicAlbum;
MyAlbums : List<MusicAlbum>;
begin
// Create our Album List
MyAlbums := new List<MusicAlbum>;
// Setup a our records
MyAlbum := new MusicAlbum;
MyAlbum.Artist := 'Kanye West';
MyAlbum.Name := '808s and Heartbreak';
MyAlbum.AlbumYear := 2008;
MyAlbums.Add(MyAlbum);
// Execute our LINQ Query
var AlbumsLastYear := from Album in MyAlbums where Album.AlbumYear.Equals(2008);
// Add the result to our listbox
for Album in AlbumsLastYear do
begin
lbFoundAlbums.Items.Add(Album.Artist + ' - ' + Album.Name + ' (' + Album.AlbumYear + ')');
end;
end;
This code clipping also demonstrates Type Inference which allows a variable type to be inferred from the context of it’s usage. For example, consider writing the following: var MyGreatList := new List<String>; or even something as simple as var s := ‘string’;. Type Inference is a useful addition which I can see myself making extensive use of. My only concern about Type Inference is how it affect’s the readability of the code as I am used to reviewing the var section of a method for a guide to what types are used within a method.
LINQ can also be used with XML. The below example takes our previous sample and reads the data from an XML file instead:
<?xml version="1.0" encoding="UTF-8"?> <albums> <album> <artist>Kanye West</artist> <title>808s and Heartbreak</title> <albumyear>2008</albumyear> </album> <album> <artist>Kanye West</artist> <title>Graduation</title> <albumyear>2007</albumyear> </album> <album> <artist>Nas</artist> <title>Unartistd</title> <albumyear>2008</albumyear> </album> <album> <artist>Notorious OST</artist> <title>The Notorious B.I.G.</title> <albumyear>2009</albumyear> </album> </albums>
var
AlbumData: XDocument;
begin
// Load our XML Document with a System.Xml.Linq.XDocument
AlbumData := XDocument.Load('../../albums.xml');
// Query for albums in the year 2008
var AlbumsLastYear := from Album in AlbumData.Descendants('album') where Album.Element('albumyear').Value = '2008' select Album;
for Album in AlbumsLastYear do
begin
var s := String.Format('{0} - {1} ({2})', Album.Element('artist').Value, Album.Element('title').Value , Album.Element('albumyear').Value);
lbFoundAlbums.Items.Add(s);
end;
Another new language feature that I like in principle but will completely admit to not fully understanding yet is Anonymous Methods and Delegates which make executing code with different threads much easier. Sadly in my brief testing of Anonymous methods I discovered that they are only available in projects which link to the .NET Framework 3.0 and above (and, in my case, only WPF projects). In order to execute our ListBox adding code from the first code sample as an Anonymous Delegate you can do the following:
// Add the result to our listbox
for Album in AlbumsLastYear do
begin
Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, method(MyAlbum : MusicAlbum);
begin
lbFoundAlbums.Items.Add(MyAlbum.Artist + ' - ' + MyAlbum.Name + ' (' + MyAlbum.AlbumYear + ')');
end, Album);
end;
This means that the compiler declares a delegate with a signature matching our anonymous method and we have now easily executed this code within a seperate thread with a minimum of code.
There are several new language features which I personally have no use for but which facilitate the use of the Microsoft Parallels framework such as Futures and Parallel sequences. Delphi Prism also supports Generics and as well as most of the standard Delphi Win32 syntax.
There are also lots of small additions that aren’t .NET features per-se but demonstrate the usefulness of freeing the Prism compiler and allowing more rapid progression such as Double Boolean Expressions. I’m sure we’ve all written a statement like this before: if (cost > 400) and (cost < 600) then. In Delphi Prism this can also be written as: if 400 < cost < 600 then.
As I quickly discovered whilst going through the Delphi Prism Wiki: the language changes to Delphi Prism were easier to adapt to and to learn than I had originally imagined. The New language features gained mostly did not affect my coding style too much so any developers who were concerned that Delphi Prism is nothing like Delphi have nothing to worry about. Many of the new features are useful for compatibility with the .NET Framework and will be a natural fit for existing Delphi or .NET Developers

I am a Delphi Developer, .NET and Web Developer and General Geek. I am an enthusiastic advocate of hobbyist development and in particular tools which allow for hobbyist development. Please have a good look around and enjoy anything that you find useful on this site. 

Hi Jamie, can you give some information about Delphi Prism and ASP.NET connecting to a SQL Server Database View, and showing values?
Nice blog, keep it up.
Thank you.
Hi Murilo,
Unfortunately I don’t do a lot of work with ASP.NET and Prism (although it is on my todo list!) so I can’t give you first hand advice however I can point you to these videos from the recent CodeRage III which will help you out IIRC.
http://dn.codegear.com/prism/Programming/ASPDotNet
Jamie