I need to implement a function which returns a TDictionary, without specifying the exact types. The returned value could be a TDictionary<string,Integer>
, TDictionary<string,string>
or TDictionary<string,Boolean>
Could I declare the function with TDictionary as result parameter:
function GetMap: TDictionary;
and then cast the return value:
type
TMyMapType: TDictionary<string,Integer>;
var
MyMap: TMyMapType:
begin
...
MyMap := GetMap as TMyMapType;
...
end;
Edit: found that there seems to be no way to declare a 'generic' result parameter type which would be type compatible with my three dictionary types.
It looks like I need something like
type
TMyMapType: TDictionary<string, ?>;
which is not (yet?) possible in the Object Pascal language. In Java it would be something like this:
static Map<String, Integer>getIntegerMap() {
Map<String, Integer> result = new TreeMap<String, Integer>() {};
result.put("foo", Integer.valueOf(42));
return result;
}
static Map<String, ?> getMap() {
return getIntegerMap();
}
public static void main(String[] args) {
System.out.println(getMap().get("foo"));
}