问题
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