Problems with converting Java code to delphi

混江龙づ霸主 提交于 2019-12-24 03:28:03

问题


I have a method that must return a generic interface. Have tried to make the method in delphi. But are unsure on how it should be written? Is there anyone who can help me? Here's an example I've made in Java that works:

public < T  extends StandardDataProvider<?>> T GetDataProvider(String dataProviderName) {
    if (dataproviders == null)
        buildDataProviderMap();
    if (dataproviders.containsKey(dataProviderName)) {
        return (T) dataproviders.get(dataProviderName);
    } else
        return null;
}

Then tried to do the same in delphi .. But can not get it to work?

function TLocalDataProviderFactory. GetDataProvider(DataProviderName: string): IStandardDataProvider;  // Shows errors here? 
begin
  if not Assigned(DataProvider) then
    BuildDataProviderMap;
  if DataProvider.ContainsKey(DataProviderName) then
  begin
    Result := DataProvider.Items[DataProviderName];
  end
  else
  begin
    Result:= nil;
  end;
end;

回答1:


Delphi generic constraints do not support wildcards. So the closest you can manage involves two generic parameters. The function would look like this:

function GetDataProvider<S; T: IStandardDataProvider<S>>(...): T;


来源:https://stackoverflow.com/questions/19774018/problems-with-converting-java-code-to-delphi

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