user1817376
user1817376

Reputation: 107

Undeclared identifier for TComparer.Construct although System.Generics.Defaults is in uses

Here is the code I was trying to learn from but I am stuck

uses
  System.Generics.Defaults, System.Generics.Collections, System.AnsiStrings,
  …

 try
      sortedDictKeys.Sort(TComparer.Construct(
       function (const L, R: integer): integer
       begin
         result := R - L;
       end
       )) ;

Undeclared identifier TComparer.Construct(

and too many actual parameters error in the above code that I was trying from about.com

that I was trying to learn about basics of generics. I am stuck here and don't know why it won't compile.

Full code is here: http://delphi.about.com/od/beginners/a/using-t-dictionary-hash-tables-in-delphi.htm

Also if anyone points me to the right direction to learn TDictionary in the generics parameters that would be great.

Upvotes: 1

Views: 2138

Answers (1)

jachguate
jachguate

Reputation: 17203

The code you show is trying to use Generics classes, but is not including the type.

I'm not sure if in a previous Delphi version the compiler infers the type from the declaration, but usually you have to declare the type, like this:

uses Generics.Defaults, Generics.Collections;

...

    sortedDictKeys := TList<Integer>.Create(dict.Keys);
    try
      sortedDictKeys.Sort(TComparer<Integer>.Construct(
       function (const L, R: integer): integer
       begin
         result := R - L;
       end
       )) ;
      for i in sortedDictKeys do
        log.Lines.Add(Format('%d, %s', [i, dict.Items[i]]));
    finally
      sortedDictKeys.Free;
    end;

I got the code from your linked about.com article and just made it to compile, but I didn't test it.

As requested in comments, the complete function looks like this:

var
  dict : TDictionary<integer, char>;
  sortedDictKeys : TList<integer>;
  i, rnd : integer;
  c : char;
begin
  log.Clear;
  log.Text := 'TDictionary usage samples';

  Randomize;

  dict := TDictionary<integer, char>.Create;
  try
    //add some key/value pairs (random integers, random characters from A in ASCII)
    for i := 1 to 20 do
    begin
      rnd := Random(30);

      if NOT dict.ContainsKey(rnd) then dict.Add(rnd, Char(65 + rnd));
    end;

    //remove some key/value pairs (random integers, random characters from A in ASCII)
    for i := 1 to 20 do
    begin
      rnd := Random(30);

      dict.Remove(rnd);
    end;

    //loop elements - go through keys
    log.Lines.Add('ELEMENTS:');
    for i in dict.Keys do
      log.Lines.Add(Format('%d, %s', [i, dict.Items[i]]));

    //do we have a "special" key value
    if dict.TryGetValue(80, c) then
      log.Lines.Add(Format('Found "special", value: %s', [c]))
    else
      log.Lines.Add(Format('"Special" key not found', []));


    //sort by keys ascending
    log.Lines.Add('KEYS SORTED ASCENDING:');
    sortedDictKeys := TList<integer>.Create(dict.Keys);
    try
      sortedDictKeys.Sort; //default ascending
      for i in sortedDictKeys do
        log.Lines.Add(Format('%d, %s', [i, dict.Items[i]]));
    finally
      sortedDictKeys.Free;
    end;

    //sort by keys descending
    log.Lines.Add('KEYS SORTED DESCENDING:');
    sortedDictKeys := TList<Integer>.Create(dict.Keys);
    try
      sortedDictKeys.Sort(TComparer<Integer>.Construct(
       function (const L, R: integer): integer
       begin
         result := R - L;
       end
       )) ;
      for i in sortedDictKeys do
        log.Lines.Add(Format('%d, %s', [i, dict.Items[i]]));
    finally
      sortedDictKeys.Free;
    end;

  finally
    dict.Free;
  end;
end;

Upvotes: 3

Related Questions