Can't use default parametr values with comparer in Spring4D

一曲冷凌霜 提交于 2019-12-11 04:06:29

问题


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?


回答1:


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());


来源:https://stackoverflow.com/questions/52742678/cant-use-default-parametr-values-with-comparer-in-spring4d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!