Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-08

Generics in FPC have many problems

8 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Anthony Walter

Jump to "So here are some of the problems I've found with fpc generics" if you don't want to read the backstory.

I haven't been active in the FPC for a while, but last week I was asking some questions about the upcoming Delphi FireMonkey project which is supposed to allow broader development with Pascal/Delphi. My first question to a newsgroup was, what about FPC, is the FireMonkey project going to work with it? I was redirected to this article:

http://wiki.freepascal.org/FPC_New_Features_Trunk

Great, these were the new features I was looking for, bridging Delphi and FPC {$mode delphi} features. So then last week I switched my laptop windows SSD for my linux SSD and got the latest sources of FPC and Lazarus sources. I wrote a new XML wrapper around both libxml and msxml, and then dove into converting my Delphi generics library. My eventual goal is to create the best cross platform game engine ever based on Delphi/Pascal.

I filled out a few bugs on the issue tracker for Lazarus where the new generic syntax for {$mode delphi} broke CodeTools. Then sent a few patches to fix those issues myself (yeah me).

But then on Saturday I started out porting my generics library, and quickly began to fall into depression. It seems FPC have a lot of problems with generics. I had to take a break from all my language testing due to all the problems I kept running into which made my head spin after each session of tryng to figure out what the problems are.

On Sunday I sat down again and started hashing out how to get by with FPC generics. In the evening I finally was able to focus in on the core problems with the way generics work in FPC.

I'd like to contribute to fixing these problems, but honestly I have no idea how to modify the compiler. I managed to send some patches off for CodeTools, but believe the compiler is a whole other level that needs gross amounts of fixings when it comes to generics. Something I'm not capable of.

Jump: So here are some of the problems I've found with fpc generics.

This does not include all the bugs that cause the compiler to act irrational one moment and a different irrational the next moment. This is simply a list of the things FPC generics can't do. I mention these rather than the generic compiler bugs, because IMO if the way generics work aren't changed, it's not even worth fixing the compiler bugs the existing syntax brings.

You can't write a generic method in a non generic class. The syntax 'TMyForm.ShowContents<T>' is invalid is invalid in both objfpc and delphi modes. You are forced to define a generic class if you want to use a generic method.

type
  TStringConverter<T> = function(Item: T): string; // okay delphi mode, in
objfpc this line should begin with generic
  TMyForm = class(TForm)
    procedure ShowContents<T>(List: TList<T>; Converter:
TStringConverter<T>);

...

// Having a generic method is not okay
procedure TMyForm.ShowContents<T>(List: TList<T>; Converter:
TStringConverter<T>);
var
  Item: T;
begin
  ListBox.Items.BeginUpdate;
  try
    ListBox.Items.Clear;
    for Item in List do
      ListBox.Items.Add(Converter(Item));
  finally
    ListBox.Items.EndUpdate;
  end;
end;

You can't use generics without specialization.

// This is not okay. Instead you have to specialize TList<T> and
TStringConverter<T> inside TMyTestObject<T>
procedure TMyTestObject<T>.DumpContents(List: TList<T>; Converter:
TStringConverter<T>);
var
  Item: T;
begin
  FStrings.BeginUpdate;
  try
    FStrings.Clear;
    for Item in List do
      FStrings.Add(Converter(Item));
  finally
    FStrings.EndUpdate;
  end;
end;

Specialization leads to problems, as specialized types from one class cannot be share with another class. Also you cannot use specialization nside an interface.

  IMyInterface<T> = interface
    function GetCurrent: T;
    // because of we specialization can't use other generics in an interface
    function CurrentString(Converter: TStringConverter<T>): string;
    procedure TakeFirst(List: TList<T>);
    property Current: T read GetCurrent;
  end;

The solution:

Get rid of the whole specialization concept in fpc. Generics should be able to be declared at any point using the type or a generic parameter. You should be able to simply say things like:

type TMyClass = class List: TList<Integer>; end;

or

type TMyClass = class procedure CountDistinct<T>(List: List<T>); end;

or

type TMyClass = class function Combine(List: List<string>): string; end;

And if someone ever does intent to fix generics in FPC, there are a whole slew of errors that they already bring into the compiler. I'll make a list of examples of those errors at a later time, but they inner workings of generics should be redone to remove the specialization requirement before those errors are addressed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110829/77064168/attachment.html>

Reply by email

Reimar Grabowski
to Anthony Walter
My eventual goal is to create the best cross platform game engine ever based on Delphi/Pascal.

Funny, many engines use C and Assembler both do not support generics. I tend to avoid them in my Java engine, too, for performance reasons.

<snip>

Show quoted text 5 lines
And if someone ever does intent to fix generics in FPC, there are a whole slew of errors that they already bring into the compiler. I'll make a list of examples of those errors at a later time, but they inner workings of generics should be redone to remove the specialization requirement before those errors are addressed.

As you say this are FPC problems not Lazarus ones. So you used the wrong list for your post. Please use one of the FPC lists to discuss compiler related stuff.

R.

A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail?

Reply by email

Anthony Walter
to Reimar Grabowski

The focus of my library is to make interactive demo writing easy. Generics, when implemented correctly, make writing code easier and more productive, given that you know what a generic is and how to use them.

Besides, when writing a (simple) game you'll likely be limited by hardware and not software. Most all the Javascript WebGL demos I've seen are limited by the drivers/hardware ... same thing goes the game systems, which have 2D/3D graphics and animation and resource/input/display/sound systems,I've written in the past.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110829/020d55d3/attachment.html>

Reply by email

Reimar Grabowski
to Anthony Walter
Besides, when writing a (simple) game you'll likely be limited by hardware and not software.

That is the sad thing about technical progress. No need to write efficient code anymore as you have plenty of cycles left on current CPUs/GPUs (not saying anything about the quality of your code). So you are absolutely correct for the case of simple games but when reading about the "best cross platform game engine ever" I was at least expecting something like Crystal Space only better. Generics have their use, no doubt, I just tend to avoid them in performance critical code and they are definitely no must have feature for game programming. But I am getting off topic...

R.

A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail?

Reply by email

Jürgen Hestermann
to Reimar Grabowski
That is the sad thing about technical progress. No need to write efficient code anymore as you have plenty of cycles left on current CPUs/GPUs (not saying anything about the quality of your code).

Then I am wondering why I am confronted with annoying slow software all day. Very simple tasks take ten to hundred the time it can take (compared to efficient programs that sometimes still exist). If CPU is idle all the time then why are so many programs so slow? I doubt that performance is no issue anymore.

Reply by email

Reimar Grabowski
to Jürgen Hestermann
Show quoted text 5 lines
Then I am wondering why I am confronted with annoying slow software all day. Very simple tasks take ten to hundred the time it can take (compared to efficient programs that sometimes still exist). If CPU is idle all the time then why are so many programs so slow? I doubt that performance is no issue anymore.

The topic was simple games not Python, Groovy, etc... ;) Performance issues in modern software are IMHO due to badly designed and coded programs (often due to lack of knowledge of low level stuff), overuse of libraries and frameworks for tasks that do not really need them, rush to market for proprietary software and most of all use of the wrong languages. Native is better. And not enough people use C++ anymore and even less the most amazing FPC.

R.

A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail?

Reply by email

Sven Barth
to Reimar Grabowski: On Mon, 29 Aug 2011 09:46:45 -0400 Funny, many engines use C and Assembler both do not…
Reimar Grabowski wrote 6 lines
> And if someone ever does intent to fix generics in FPC, there are a whole > slew of errors that they already bring into the compiler. I'll make a list > of examples of those errors at a later time, but they inner workings of > generics should be redone to remove the specialization requirement before > those errors are addressed. As you say this are FPC problems not Lazarus ones. So you used the wrong list for your post. Please use one of the FPC lists to discuss compiler related stuff.

I don't know whether you've noticed it already, but Anthony posted to both lists, Lazarus and FPC?Pascal at once (take a look at the receiver list of his mail).

Regards,
Sven

Reply by email

Reimar Grabowski
to Sven Barth
I don't know whether you've noticed it already, but Anthony posted to both lists, Lazarus and FPC?Pascal at once (take a look at the receiver list of his mail).

I did and it does not make the Lazarus list more appropiate for the topic. Looks like I am getting old as I did not know that cross-posting is nowadays an excuse for using wrong lists. I should have added an "only" in my reply. Sorry for the oversight.

R.

A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail?

Reply by email