Lazarus

Free Pascal => General => Topic started by: Vaytl on January 10, 2022, 10:45:00 pm

Title: Generics.Collections
Post by: Vaytl 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.  
Title: Re: Generics.Collections
Post by: marcov 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?
Title: Re: Generics.Collections
Post by: Vaytl 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"
Title: Re: Generics.Collections
Post by: Remy Lebeau 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.
Title: Re: Generics.Collections
Post by: PascalDragon 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?
Title: Re: Generics.Collections
Post by: Thaddy 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"
Title: Re: Generics.Collections
Post by: PascalDragon 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?
Title: Re: Generics.Collections
Post by: Vaytl 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
Title: Re: Generics.Collections
Post by: bytebites 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
Title: Re: Generics.Collections
Post by: Thaddy 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
Title: Re: Generics.Collections
Post by: PascalDragon 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... :-[
Title: Re: Generics.Collections
Post by: Vaytl 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.
Title: Re: Generics.Collections
Post by: Thaddy 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.
TinyPortal © 2005-2018