Recent

Author Topic: The right implementation of generics  (Read 22400 times)

simone

  • Hero Member
  • *****
  • Posts: 573
The right implementation of generics
« on: May 26, 2017, 11:13:27 am »
Often I use generics in my programs, but I suspect that they cause internal errors during compilation phase (almost random and difficult to reproduce, but frequent...). I apoligize if my hypothesis is pointless... Indeed I can't dimonstrate a cause-effect relation between generics and internal errors, but in the past I read in the forum users reporting similar problems. I work with FGL, since this is the implementation cited in the language reference of freepascal and because i have read that FGL is the FPC way to generics, whereas rtl-generics is only for delphi compatibility. Conversely, I have also read that rtl-generics is better. So, in order to avoid problems, what is the implementation of generics that I should use? Thanks for the help.

Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: The right implementation of generics
« Reply #1 on: May 26, 2017, 11:50:57 am »
FGL is an early effort regarding generics in FPC and on its own just works, but in trunk we have rtl-generics and that is, in many ways, faster, better and more stable.
So if it is possible for you to use trunk, please use trunk and use rtl-generics.
Note that the syntax etc for the compiler is already present (back-ported) in FPC 3.0.2, I believe, but the library itself not afaik.
« Last Edit: May 26, 2017, 11:53:13 am by Thaddy »
Specialize a type, not a var.

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: The right implementation of generics
« Reply #2 on: May 26, 2017, 11:53:04 am »
As weird as it may sound,  I suggest you use rtl generics especially for Delphi compatibility  and ease of use.  O:-)

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: The right implementation of generics
« Reply #3 on: May 26, 2017, 11:54:06 am »
As weird as it may sound,  I suggest you use rtl generics especially for Delphi compatibility  and ease of use.  O:-)
It is not weird. Everybody agrees that rtl-generics is superior to fgl. And the implementation is in my opinion superior to Delphi too... Thanks to Maciej (HNB).
« Last Edit: May 26, 2017, 11:59:38 am by Thaddy »
Specialize a type, not a var.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: The right implementation of generics
« Reply #4 on: May 26, 2017, 12:06:42 pm »
Thanks for explanations. At this point I have other questions (may be silly... thank for patience!):

1) if rtl-generics is superior to fgl, why fpc comes with fgl and language reference only refers to it?

2) can fgl cause internal errors during compilation? If yes, this problem don't arise with rtl-generics?

3) I understand I need to migrate my code to rtl-generics. This unit guarantee some form of backward compatibility with fgl (in particular for TFPGObjectList)?
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: The right implementation of generics
« Reply #5 on: May 26, 2017, 12:14:43 pm »
Thanks for explanations. At this point I have other questions (may be silly... thank for patience!):

1) if rtl-generics is superior to fgl, why fpc comes with fgl and language reference only refers to it?
Time...very simple... and a complete years long discussion on the development forum...and loads of compiler improvements and additions...
Quote
2) can fgl cause internal errors during compilation? If yes, this problem don't arise with rtl-generics?
Yes and no. Internal errors will almost always be solved and will be solved very quick. Internal errors should not occur. The sooner you give a bug report, the quicker it will be solved, usually.
Quote
3) I understand I need to migrate my code to rtl-generics. This unit guarantee some form of backward compatibility with fgl (in particular for TFPGObjectList)?
No. The implementation in rtl-generics  is completely different from the list in fgl.
The interface, the way you use it, is compatible with Delphi.
The syntax is almost similar, but not quite. Even a beginner should be able to translate any differences within minutes.
Specialize a type, not a var.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: The right implementation of generics
« Reply #6 on: May 26, 2017, 12:50:15 pm »
Thanks for precise explanations. About point n.2: internal errors happen in my ongoing project. Now it reaches 10Kloc and is difficult for me to isolate errors and reproduce them outside. I noticed that internal errors arise after I change something in data structure definitions, in the type section of units. After the error notification of compiler, if I purge the lib directory, with files .o, .ppu etc., in the next compilation the error disappears. To help developer team and community, now i will work to reproduce errors... I hope this will allow to fix problems...
« Last Edit: May 26, 2017, 12:52:49 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: The right implementation of generics
« Reply #7 on: May 26, 2017, 01:37:38 pm »
I think you're talking about Internal error 2014052302 in FPC 3.0.0.
Install FPC 3.0.2. It's fixed there.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: The right implementation of generics
« Reply #8 on: May 26, 2017, 01:44:27 pm »
Often I use generics in my programs, but I suspect that they cause internal errors during compilation phase (almost random and difficult to reproduce, but frequent...). I apoligize if my hypothesis is pointless... Indeed I can't dimonstrate a cause-effect relation between generics and internal errors, but in the past I read in the forum users reporting similar problems.

Sorry for the off-topic but that remembers me a lot what happened with C++ templates when I used C++, and it was the main reason I decided not to use such language again.  It was a lot of time ago, pre C99 IIRC.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

simone

  • Hero Member
  • *****
  • Posts: 573
Re: The right implementation of generics
« Reply #9 on: May 26, 2017, 01:59:42 pm »
I already use FPC 3.0.2... The last arised is 2011031501... but this is not the only one that I periodically see...

To overcome the problem, that is becoming very annoying for me, I will migrate to rtl-generics found in

https://github.com/graemeg/freepascal/tree/master/packages/rtl-generics

I see only some examples... Where can I read some documentation about rtl-generics?
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: The right implementation of generics
« Reply #10 on: May 26, 2017, 02:12:44 pm »
https://github.com/graemeg/freepascal/tree/master/packages/rtl-generics

I see only some examples... Where can I read some documentation about rtl-generics?

the above repo is just one of mirrors for FPC svn. The original repo for FPC Generics.Collections / rtl-generics is:

https://github.com/dathox/generics.collections

you can use documentation from embarcadero:
http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Generics.Collections

The compatibility matrix:
https://github.com/dathox/generics.collections/blob/master/GenericsCompatibilityMatrix.pdf
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: The right implementation of generics
« Reply #11 on: May 26, 2017, 02:21:41 pm »
Tnx. and many thanks for doing it properly... (you and Sven)
« Last Edit: May 26, 2017, 02:32:01 pm by Thaddy »
Specialize a type, not a var.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: The right implementation of generics
« Reply #12 on: May 26, 2017, 02:27:48 pm »
Thanks for information and for the work. A last doubt: i have to compile in delphi mode?
« Last Edit: May 26, 2017, 03:20:11 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: The right implementation of generics
« Reply #13 on: May 26, 2017, 02:34:52 pm »
Nope, works also in objfpc mode. But that is by me highly discouraged (objfpc).
So just use whatever syntax you want,

The nice thing about FPC is that it converges to dwim...

So many options and so many ways to express it.
(I hope you know what dwim means)
« Last Edit: May 26, 2017, 02:39:49 pm by Thaddy »
Specialize a type, not a var.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: The right implementation of generics
« Reply #14 on: May 26, 2017, 02:44:52 pm »
Yes I know! Thanks for all explanations!
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018