Recent

Author Topic: Generics.Collections  (Read 4966 times)

Vaytl

  • New member
  • *
  • Posts: 8
Generics.Collections
« on: January 10, 2022, 10:45:00 pm »
Hi.
I am trying to migrate a project from Delphi to Lazarus.
I came across a generic, and I can't understand how it works at all. Is there some way to replace this code to opt out of "Generics.Collections"?
Code: Pascal  [Select][+][-]
  1. private
  2.  class var Dictionary: TDictionary<DWORD_PTR, Boolean>;
  3. ..........
  4. ..........
  5. if not TMyprogramm.Dictionary.TryGetValue(Context, LBool)  then exit;
  6. ..........
  7. ..........
  8. Dictionary.TryAdd(DWORD_PTR(fContext), True);
  9. ..........
  10. ..........     
  11. initialization
  12.   TMyprogramm.Dictionary := TDictionary<DWORD_PTR, Boolean>.create;
  13.  

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Generics.Collections
« Reply #1 on: January 10, 2022, 10:53:53 pm »
It should work, but DWORD_PTR is a Windows specific type.

Are you compiling on Windows, and what is your exact errormessage?

Vaytl

  • New member
  • *
  • Posts: 8
Re: Generics.Collections
« Reply #2 on: January 10, 2022, 11:01:33 pm »
Are you compiling on Windows, and what is your exact errormessage?

Yes, I compiling on windows.
Error: identifier idents no member "tryAdd"

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Generics.Collections
« Reply #3 on: January 11, 2022, 01:09:07 am »
Error: identifier idents no member "tryAdd"

FPC's TDictionary does not have a TryAdd() method.  Delphi's TDictionary has had TryAdd() for only a few years (since 10.3 Rio), so likely hasn't made its way into FPC yet.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Generics.Collections
« Reply #4 on: January 11, 2022, 09:43:34 am »
Error: identifier idents no member "tryAdd"

FPC's TDictionary does not have a TryAdd() method.  Delphi's TDictionary has had TryAdd() for only a few years (since 10.3 Rio), so likely hasn't made its way into FPC yet.

Would someone please report a bug so that it's not forgotten?

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Generics.Collections
« Reply #5 on: January 11, 2022, 10:04:53 am »
TryAdd IS available through TOpenAddressing, which is an abstract ancestor  of TDictionary.
See line 265 in generics.dictionariesh.inc
The implementation is on line 610 of generics.dictionaries.inc
Note TryAdd is public, so it is not hidden.

So I can't explain Error: identifier idents no member "tryAdd"
« Last Edit: January 11, 2022, 10:15:29 am by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Generics.Collections
« Reply #6 on: January 11, 2022, 10:14:22 am »
TryAdd IS available through TOpenAddressing, which is an abstractancestor  of TDictionary.
See line 265 in generics.dictionariesh.inc
The implementation is on line 610 of generics.dictionaries.inc

Indeed, it should be available since 3.2.2, considering I committed that myself. :-[

@Vaytl: what version of FPC/Lazarus are you using?

Vaytl

  • New member
  • *
  • Posts: 8
Re: Generics.Collections
« Reply #7 on: January 11, 2022, 05:20:03 pm »
@Vaytl: what version of FPC/Lazarus are you using?
Lazarus IDE v2.2.0RC1 r65419 X64
FPC: 3.2.2

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: Generics.Collections
« Reply #8 on: January 11, 2022, 08:54:39 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode delphi}
  3. uses Generics.Collections;
  4. var d:TDictionary<byte,byte>;
  5.     i:byte;
  6. begin
  7.   d:=TDictionary<byte,byte>.create;
  8.   d.tryadd(3,4);
  9.   if d.trygetvalue(3,i) then writeln(i);
  10.   d.free;
  11. end.
  12.          
This compiles with FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Generics.Collections
« Reply #9 on: January 11, 2022, 10:07:32 pm »
Lazarus IDE v2.2.0RC1 r65419 X64
FPC: 3.2.2
Think again, that does not make sence.
Note that you should focus on fpc. Lazarus has nothing to do with this.
TryAdd simply works, as demonstrated above.
If the example from Bytebites fails you have an old version and not a release version of 3.2.2..

BTW @Sarah: You have a sneaky way to implement features....Respect. :o ::) :-X
« Last Edit: January 11, 2022, 10:35:05 pm by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Generics.Collections
« Reply #10 on: January 12, 2022, 09:36:06 am »
@Vaytl: what version of FPC/Lazarus are you using?
Lazarus IDE v2.2.0RC1 r65419 X64
FPC: 3.2.2

Please try the example provided by bytebites. If that works the error is somewhere else in the code you want to convert (can you maybe post the full source, or is it available somewhere public?). If not then you'd need to check whether you're really using FPC 3.2.2.

BTW @Sarah: You have a sneaky way to implement features....Respect. :o ::) :-X

Sneaky enough that I didn't remember it myself. :D Then again I only applied a patch... :-[

Vaytl

  • New member
  • *
  • Posts: 8
Re: Generics.Collections
« Reply #11 on: January 12, 2022, 11:23:10 am »
I copied the files:
inc\
generics.collections.pas
generics.defaults.pas
generics.hashes.pas
generics.helpers.pas
generics.memoryexpanders.pas
generics.strings.pas
to the project folder, after which the error disappeared. Thanks everyone.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Generics.Collections
« Reply #12 on: January 12, 2022, 11:50:33 am »
That is not a real solution. That means an older version of generics.collections is in your path. You need to solve that first.
Specialize a type, not a var.

 

TinyPortal © 2005-2018