Search code examples
delphidelphi-10.2-tokyospring4d

Can't use default parametr values with comparer in Spring4D


I'm not sure if this is some generic problem or it's because of Spring4D implementation, but I can't use default parameter values for creating comparer.

type
  TMyClass = class
    class function MyComparer(AParam: Boolean = False): IComparer<TMyClass>;
  end;

implementation

class function TMyClass.MyComparer(AParam: Boolean): IComparer<TMyClass>;
begin
  Result := TComparer<TMyClass>.Construct(
    function (const L, R: TMyClass): Integer
    begin
      Result := 0;
    end);
end;

When I create a list without the specified parameter, I get an error message about missing parameters.

TCollections.CreateSortedObjectList<TMyClass>(TMyClass.MyComparer);

E2035 Not enough actual parameters

However without any parameters or with all parameters specified it works. Is there any reason why I can't do that?


Solution

  • I don't have Spring4D to hand to test, but I'm guessing that what's happening is something similar to this where Delphi's syntax rules allowing omission of parentheses when executing a method which takes no parameters introduces ambiguity. Here, where you do :

     TCollections.CreateSortedObjectList<TMyClass>(TMyClass.MyComparer);
    

    ...the compiler can't be sure if you mean to pass the method MyComparer directly (to the overload of CreateSortedObjectList which takes a method pointer type TComparison<T>) or whether you mean to execute the method and pass the return value. In this case you want to do the latter, so you can be explicit for the compiler and include the parentheses

     TCollections.CreateSortedObjectList<TMyClass>(TMyClass.MyComparer());