4

As the announcements have recently proclaimed: Delphi Prism 2011 is out now and the Software Assurance emails went out particularly quickly after the announcements (great work team embarcadero!). I thought I might share what I’ve found to be new and cool in the Delphi 2011 Release.

Visual Studio 2010 Support

I’ll start with the most obvious change: Delphi Prism now supports Visual Studio 2010 which was only released just over a month ago. The Visual Studio 2010 IDE looks great, feels faster than 2008 and most importantly, in my experience, is very stable.

Visual Studio 2010 with Delphi Prism

The shell is based on WPF and has had considerable work put into allowing it to be flexible and extensible which I understand has allowed a whole host of new plugins to flourish. It’s an extremely minor point but I’ve also found it much better in allowing themes for the code editor, springing forth sites like http://studiostyles.info/. My favourite scheme  is Son of Obsidian which works marvellously for C# code and works reasonably well for Delphi Prism code too. It’s great that we can take advantage of community extras like this, although it would also be great if we could tailor some of these themes specifically for Delphi Prism.

As marc mentioned back on episode #39 of the Podcast at Delphi there are a lot of free minor improvements that come with moving to Visual Studio 2010 (not financially free but that were inherent with the migration).

Other cool stuff includes support for Silverlight 4 and the “Paste C# as Oxygene” feature which allows you to translate code snippets on the fly. This release also includes MonoDevelop which includes support for Mac and Windows.

For, If and Case Expressions

For, if and case I hear you say? Haven’t we had those for as long as we can remember? Not like this we haven’t. Delphi Prism now supports them as expressions. This allows you to do operations like this:

  var sourceList: Array of Integer := [1 ,2 ,4 ,8 ,16 ,32 ,64 ,128];
  var sqList := for each num in sourceList yield num * num;

Now this sample could just as easily be done with a LINQ operation or a separate loop but this is another option available at your disposal and introduces a slightly Functional Programming feel. Delphi Prism now also supports If expressions and my favourite expression addition: case.

These expressions, as with so many language features hold the potential for misuse but they can also make things a little more concise:

method NotifierStr(newMails: integer): string;
begin
  result := 'You have ' + newMails.ToString() + ' new ' +
    case newMails of
      0: 'emails';
      1: 'email';
    else
      'emails';
    end;
end;

Please forgive the horrible string concatenation but I wanted to demonstrate it in this way because I thought it might help make it a little clearer.

Dynamic

Delphi Prism 2011 includes support for the (in some cases) contraversial .NET 4 dynamic type. To quote Scott Hanselman on dynamics: A dynamic is a statically typed dynamic type. Dynamics leave a lot more till run-time and allow a much cleaner interop with the Dynamic Scripting languages built upon the CLR.

For example, the ExpandoObject enables you to add and delete members at runtime, something might be useful when serialising and deserialising JSON or XML where you do not know the spec ahead of time:

dynamic tweetresult := new ExpandoObject();
tweetresult.Id = 1243535345;
tweetresult.Username = 'delphifeeds';

The Delphi Prism 2011 implementation has a required dependency upon a separate assembly, RemObjects.Oxygene.Dynamic.dll, which is freely re-distributable.

For a much better idea of what dynamics are and can be used for, I would refer you to Scott Hanselman’s whirlwind tour of dynamics and the MSDN reference on using dynamics.

Tail Calls

Tail Calls are another feature that could be seen as similar to a Functional Programming Language construct. They are essentially an exit statement followed by a call to a method. They have to be explicitly enabled and disabled for a method using the {$TAILCALL ON} flag. This allows you to code recursive functions, particularly useful for maths based methods. This concept will probably take a bit of getting used to and are something I’d like to cover in subsequent posts.

Tuples

Tuple is an ordered list of Elements. They can contain 1 to 7 different references and types with an 8th reference being used to reference another Tuple if you need to hold more than 7. They are similar to an array and a class/record but unlike an array can contain more than one type and require considerably less declaration than a class/record. A Tuple in Delphi Prism might be used as follows:

  var telephoneList := new Dictionary<Tuple<string, integer>, String>();
  telephoneList.Add(Tuple.Create('Josh Moore', 1283843), '+0014158343131');
  Console.WriteLine(telephoneList[Tuple.Create('Josh Moore', 1283843)]);

They are particularly useful for returning more than one value from a method where you would otherwise need to explicitly declare a class or record to hold the data otherwise. Tuples are a good way to solve a specific problem and will be more familiar to those who have used them in other languages such as the Tuple support in Python.

Parallel Loops (not actually a new feature)

Parallel loops have been a part of the Delphi Prism (and formerly Oxygene) language for a long, long time. However what I hadn’t appreciated was that the Parallel Task Library upon which it relied was officially bundled into the .NET 4.0 Framework libraries. As such I felt it necessary to re-raise it and show just how nifty it is here even though it isn’t really new to Delphi Prism 2011.

Just to recap, with the parallel keyword you can parallelise a loop.

As a fully fledged .NET 4 language we can also use the AsParallel() method in order to return a ParallelEnumerable source capable of being queried with the .NET Parallel LINQ library.

The rough and ready example below demonstrate these two techniques:

class method ConsoleApp.Main(args: array of string);
begin
  var anagramletters := 'mite';
  var filepath := 'mydictionary.txt';

  var dictList := new List<string>();
  dictList.AddRange(System.IO.File.ReadAllLines(filepath));
  Console.WriteLine('Added ' + dictList.Count + ' lines to the file');

  var sortedList := new Dictionary<string, string>();

  for parallel i: Integer := 0 to dictList.Count - 1 do
  begin
    sortedList[dictList.Item[i]] := dictList[i].Sort();
  end;

  var matchingwords := sortedList.AsParallel().Where(w -> ((w.Value.Length = anagramletters.Length) and (w.Value = anagramletters.Sort()))).Select(w -> w.Key);

  Console.WriteLine('Found: ' + matchingwords.Count().ToString() + ' results');
  matchingwords.ToList().ForEach(x -> Console.WriteLine(x));

  Console.ReadLine;

end;

The code above reads a dictionary file in and then builds an index using a parallel loop before finding the words that are anagrams of the provided string using a Parallel LINQ query.

Extension Methods

I consider it a slightly more minor point as I’ve already briefly talked about the new method of defining Extension Methods, but this is now baked into Delphi Prism 2011 and is a pretty useful tool to have. I used the new Extension Methods syntax in the Parallel Example above to add an implementation specific Sort() method to my strings like so:

interface

extension method String.Sort : String;

implementation

extension method String.Sort : String;
begin
  var c := self.ToCharArray();
  Array.Sort(c);
  result := new string(c);
end;

Summary

Overall Delphi Prism 2011 is a pretty significant release and contains lots of new features that represent a lot of interesting new potential both on the language side and the IDE side, congratulations to the RemObjects team.

Further Reading

Tags: , ,

4 Comments

  1. Jim McKeeth on the 11th June 2010 remarked #

    Excellent summary and explanation of the new features of Delphi Prism 2011. While Tuples aren’t specific to functional languages, I was under the impression they were a huge benefit for them.

  2. jamiei on the 11th June 2010 remarked #

    Thanks Jim.

    I’m slowly starting to break into the Functional Programming mindset but think you might be right about Tuples. That might also explain why some (normally oo) programmers simply don’t see the need for them.

  3. Mark Johnson on the 15th July 2011 remarked #

    I agree with Jim Mckeeth. Indeed excellent summary of what’s new in Delphi Prism 2011. Although I am a beginner in this programming playing field, I am learning a lot from blogs like yourself. Thank you and keep up the good work.

Leave a Comment