A pleasant surprise with Delphi XE

I liked Delphi XE when I tried it out, but I still haven’t really moved all my work over to it. It takes a lot of setup to migrate everything, and I’ve been busy with other stuff lately. So I was doing come coding in D2010 last night, and I ran into an annoying error. Here’s a simplified version. This really ought to compile, but the compiler choked on it.

<pre>
program Project3;

{$APPTYPE CONSOLE}

uses
  SysUtils;

procedure test(objs: TArray<TObject>);
begin
  writeln(high(objs)); //[DCC Error] Project3.dpr(10): E2008 Incompatible types
end;

var
  arr: TArray<TObject>;
begin
  try
    setLength(arr, 5);
    test(arr);
    readln;
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do
      Writeln(E.ClassName, ‘: ‘, E.Message);
  end;
end.
</pre>

TArray is simply defined as “array of T”, and ought to behave like a dynamic array. But when it’s a parameter to a function call, and you try to call High on it, the compiler doesn’t want to treat it like an array.

At first I was a bit resigned. You sorta get used to it when you’ve spent two years working with a Generics system that’s kinda broken. You learn its quirks and the tricks that usually work to work around it. A couple things came to mind that would probably fix it.

Then I thought, I should test this in XE first. So I tried it, and it works now. Embarcadero says they fixed 87 Generics-related problems in Delphi XE. I guess this is one of them. So now it’s time to migrate…

13 Comments

  1. Andy says:

    Just do not use generics. Generics is the evil of OOP.

  2. I wonder what could possibly be evil about generics?

  3. Mason Wheeler says:

    Maybe he’s just used to the C++ way, which definitely counts. 😉

  4. Andy says:

    You can not declare variable such as var a: TFoo – it is contradicts the principle of polymorphism.
    I faced with a similar situation – had to use interfaces.

  5. Mason Wheeler says:

    Andy: You have to use HTML escaping to write angle brackets in comments.

  6. Please explain how “it contradicts the principle of polymorphism”. And please specify, what is the “principle of polymorphism”? Are you referring to the LSP?

  7. Dany says:

    There’s a LOAD of generic-realted problems in D2010 IDE. They should really backport the fixes. Class comlpletion is totally thrown by generics and they have closed the issues in QC! Because it’s fixed in XE. That’s a mean way of making revenue. Sucks. Embarcadero will have to take care of their customers (loyal, using D2010) or go apple-style (and thay cant). Buh!

  8. Dany says:

    Ok, sorry. What threw off my rant (above) was this posts title. Calling a bugfix “pleasant surprise” when it’s really something that should be fixed without having to pay for a new version… oh, my…

  9. Ron Grove says:

    I honestly don’t understand hostility towards generics. Now, I admit I’ve had some hostility towards Embarcadero’s implementation of them since D2009 arrived, but even if you only used them for typesafe lists they rock. I’m glad they’re getting better, agree they should be giving D2009/2010 users as many of those bug fixes as possible.

  10. Michael Thuma says:

    Agree with Danny – Backports defintly make sense. In the current case … on the old compiler … I think it is wise to choose the lastest version XE … But from the next releases on EMB should consider backports. The Samba backports of the German Samba community are highly welcome and under other OSes that Windows this is something just normal.

  11. Steven says:

    While back-porting would be nice, I would rather them focus on the newer features that we can use to leverage in the next release. Besides, thats what software assurance is for, to insure that you get the latest fixes in the latest release. If your using Generics and run into a bug which has been fixed in the latest version, upgrade or work around the problem. Placing resources on back-porting to fix what has already been fixed to me is a waste. I would rather have 64bit and the cross compile library in my hands, as it is what my customers desire. Besides, the source changes required to upgrade a Delphi 2009/2010 project should be extremely minimal.

  12. Marco van de Voort says:

    Generics starts getting better and better in XE. There are still several problems with type aliases though. ( TYPE something = T; inside a generic class, and then use it)

    What really strikes me is the total absence of anything using “string” in the default generics uses. I hope this doesn’t mean that strings and generics is a nono (I’m still exploring the concept)