Recent

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

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: The right implementation of generics
« Reply #30 on: June 10, 2017, 03:50:50 pm »
*sigh*

To make it more clear: currently you can only influence the generic syntax if you do this:

Code: Pascal  [Select][+][-]
  1. {$mode delphi}

or

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}

Once I'm done you'll be able to do this:

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. {$modeswitch generickeywords}

or

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$modeswitch generickeywords off}

That definitely doesn't work currently! (because currently there is no modeswitch generickeywords)

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: The right implementation of generics
« Reply #31 on: June 12, 2017, 10:49:43 am »
That definitely doesn't work currently! (because currently there is no modeswitch generickeywords)

with this modeswitch objfpc mode will be almost usable  ;)
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: The right implementation of generics
« Reply #32 on: June 12, 2017, 11:36:57 am »
That definitely doesn't work currently! (because currently there is no modeswitch generickeywords)

with this modeswitch objfpc mode will be almost usable  ;)

Now, just get rid of those redundant @'s, and mandatory ()'s  >:D

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: The right implementation of generics
« Reply #33 on: June 12, 2017, 01:39:51 pm »
Nice to here about rtl-generics!  :)
Until today i only used fgl and made my own thread-safe lists.
Maybe someone should give a hint in the wiki!?

By the way: Why aren't generic type names begining with a capital G instead of a T? This would make it easier to
distinguish generic types from other types.
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: The right implementation of generics
« Reply #34 on: June 12, 2017, 04:53:57 pm »
@Marcov: I really don't want to remind you (especially when in grumpy mode ;) ) that there is a modeswitch at least for the @-dereferencing ...
[edit]Maybe the Sparta-examples can be moved to the Examples-Directory that way they are found so there are less questions about a wiki that also woud be a good thing ... (Pascal +2 f.m.)
... and if you want to do me a favor use my version: (
    https://github.com/joecare99/Public/tree/master/Examples/Source/Sparta * (the units)
and https://github.com/joecare99/Public/tree/master/Examples/FPC T*.lp* (The Project-files)
and https://github.com/joecare99/Public/tree/master/Examples Sparta_ex.lpg (The Project-Group)
)
@Pascal: if you don't want to wait for rtl-generics use sparta-generics which is already shipped with the current version.
BTW: The Sparta-Generics-Examples work (and don't work) with both sparta & rtl except one (THashMapCaseInsensitive) that compiles but throws an Exception
Code: [Select]
Working with case insensitive THashMap

Add Cat and Dog
An unhandled exception occurred at $000000010002F744:
EAccessViolation: Access violation
  $000000010002F744 line 3230 of generics.defaults.pas
  $0000000100018218 line 1275 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $000000010001950F line 1429 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000172F6 line 44 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000016D3 line 10 of THashMapCaseInsensitive.lpr
  $00000001000016E6
  $0000000100012203
  $0000000100012D31
  $00007FF9166F8364
  $00007FF9169A70D1

Heap dump by heaptrc unit
109 memory blocks allocated : 8528/8752
98 memory blocks freed     : 7456/7680
11 unfreed memory blocks : 1072
True heap size : 524288 (192 used in System startup)
True free heap : 521376
Should be : 521616
Call trace for block $00000000000EFBE0 size 40
  $000000010000D1FB
  $0000000100012722
  $000000010001282C
  $00007FF9169EAB9D
  $00007FF916989913
  $00007FF9169E9CBA
  $000000010002F744 line 3230 of generics.defaults.pas
Call trace for block $0000000000104F70 size 128
  $0000000100014186
  $000000010000D1DB
  $0000000100012497
  $0000000100012706
  $000000010001282C
  $00007FF9169EAB9D
  $00007FF916989913
Call trace for block $000000000015CFD0 size 176
  $000000010000D162
  $000000010000A657
  $00000001000505AA
  $0000000100052D6C
  $00000001000126D7
  $000000010001282C
  $00007FF9169EAB9D
Call trace for block $00000000000FD380 size 80
  $000000010000D162
  $000000010000A062
  $0000000100018054 line 1322 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000194E3 line 1427 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000172F6 line 44 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000016D3 line 10 of THashMapCaseInsensitive.lpr
  $00000001000016E6
Call trace for block $00000000000FD2A0 size 80
  $000000010000D162
  $000000010000A062
  $0000000100018054 line 1322 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000194E3 line 1427 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000172F6 line 44 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000016D3 line 10 of THashMapCaseInsensitive.lpr
  $00000001000016E6
Call trace for block $00000000000FD1C0 size 80
  $000000010000D162
  $000000010000A062
  $0000000100018054 line 1322 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000194E3 line 1427 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000172F6 line 44 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000016D3 line 10 of THashMapCaseInsensitive.lpr
  $00000001000016E6
Call trace for block $00000000000FD0E0 size 80
  $000000010000D162
  $000000010000A062
  $0000000100018054 line 1322 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000194E3 line 1427 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000172F6 line 44 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000016D3 line 10 of THashMapCaseInsensitive.lpr
  $00000001000016E6
Call trace for block $000000000010D060 size 104
  $000000010000D162
  $000000010000A657
  $000000010001ECED line 65 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $0000000100019066 line 1196 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000191AA line 1214 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000172A5 line 41 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000016D3 line 10 of THashMapCaseInsensitive.lpr
Call trace for block $00000000000EFA60 size 48
  $000000010000D162
  $000000010000A657
  $000000010001C7B5 line 647 of generics.collections.pas
  $0000000100017A3B line 1148 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $000000010001BBBD line 71 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $000000010001BAB2 line 66 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $0000000100019040 line 1195 of ../Source/Sparta/uHashMapCaseInsensitive.pas
Call trace for block $000000000010CF60 size 104
  $000000010000D162
  $000000010000A657
  $000000010001BA7D line 65 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $0000000100019040 line 1195 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000191AA line 1214 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000172A5 line 41 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000016D3 line 10 of THashMapCaseInsensitive.lpr
Call trace for block $000000000015CE90 size 152
  $000000010000D162
  $000000010000A657
  $0000000100019171 line 1213 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000172A5 line 41 of ../Source/Sparta/uHashMapCaseInsensitive.pas
  $00000001000016D3 line 10 of THashMapCaseInsensitive.lpr
  $00000001000016E6
  $0000000100012203
« Last Edit: June 12, 2017, 05:09:58 pm by jc99 »
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: The right implementation of generics
« Reply #35 on: June 12, 2017, 05:05:02 pm »
except one (THashMapCaseInsensitive) that compiles but throws an Exception
Which a reported and acknowledged bug. There are one or two more.
Specialize a type, not a var.

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: The right implementation of generics
« Reply #36 on: June 12, 2017, 05:24:08 pm »
@Pascal: if you don't want to wait for rtl-generics use sparta-generics which is already shipped with the current version.
Thanks. Not realy a problem as i already use trunk version of fpc and lazarus.  :D
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: The right implementation of generics
« Reply #37 on: June 12, 2017, 05:36:23 pm »
except one (THashMapCaseInsensitive) that compiles but throws an Exception
Which a reported and acknowledged bug. There are one or two more.
I Know, the interesting thing I try to mention is that the exception happens on both (Rtl & Sparta)
( at the same line at the same piece of code )
... so if it works, it works on both, if not also on both. (at least for now).
to reduce the lack of documentation I started a thread:
http://forum.lazarus.freepascal.org/index.php/topic,37161.msg248909.html#msg248909

@Pascal: ... but maybe the examples are useful ...


 
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: The right implementation of generics
« Reply #38 on: June 12, 2017, 06:17:15 pm »
Is there a generic AVLTree or ThreadAVLTree in there somewhere?
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: The right implementation of generics
« Reply #39 on: June 12, 2017, 08:33:49 pm »
Is there a generic AVLTree or ThreadAVLTree in there somewhere?
Not that I Know of
I only saw a Hashlist, Hashes are another method of organizing a list, and normaly really fast.
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: The right implementation of generics
« Reply #40 on: June 13, 2017, 06:21:18 am »
I am already implementing my own, based on Laz_AVL_Tree.

What about my proposed naming conventon with a capital G instead of T for generic classes?
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: The right implementation of generics
« Reply #41 on: June 13, 2017, 08:23:53 am »
Quote
What about my proposed naming conventon with a capital G instead of T for generic classes?

That would confuse me  8-)
If you mean T as generic type descriptor.
Because T stands for TYPE and once T is known, the code is no longer generic, but typed. So I think it is not a good idea.
For classes, same thing.
Also it would not be a Delphi compatible convention even if it is a naming convention only.
« Last Edit: June 13, 2017, 08:27:25 am by Thaddy »
Specialize a type, not a var.

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: The right implementation of generics
« Reply #42 on: June 13, 2017, 08:43:53 am »
No, not G for the generic type descriptor. I meant G for the generic class name:

Code: Pascal  [Select][+][-]
  1. type
  2.   generic GAVLTree<T> = class(TAVLTreeBase)
  3.   ...
  4.   end;
  5.  
  6.   TTypAVLTree = specialize GAVLTree<TTyp>;
« Last Edit: June 13, 2017, 08:46:08 am by Pascal »
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: The right implementation of generics
« Reply #43 on: June 13, 2017, 08:51:31 am »
Well. As I wrote: It is not a convention that I like (also the syntax mode, btw) and rtl-generics uses TgenericClassName and Delphi mode generics.  Would mean I have to maintain different code bases for Delphi and FPC.                                   
Specialize a type, not a var.

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: The right implementation of generics
« Reply #44 on: June 13, 2017, 08:58:44 am »
G prefix is sometimes used for global variable (personally I don't use this convention).
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

 

TinyPortal © 2005-2018