Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 517919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:53:02+00:00 2026-05-13T07:53:02+00:00

I need to implement a function which returns a TDictionary, without specifying the exact

  • 0

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"));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:53:03+00:00Added an answer on May 13, 2026 at 7:53 am

    No, there’s no “base TDictionary class that all TDictionary versions descend from”. The parameter types are part of the class type. The parent class of TDictionary<T, U> is TEnumerable<TPair<T, U>>, and the parent class of that is TObject.

    This is a bit annoying, but it’s necessary to preserve type safety. Lets say you had a TDictionary<string, TMyObject>, and you passed it to a function that’s expecting a TDictionary<string, TObject>. You might expect this to work, since you can pass a TMyObject to a TObject parameter. But it doesn’t, and there’s a good reason.

    The compiler can’t check the actual type inside the receiving function at compile-time, so there’s nothing stopping the routine from taking your dictionary and calling .Add(Self.Name, Self), where Self is a TForm (or anything else) and not a TMyObject. Since all object references are sizeof(pointer) in size, this would seem to work just fine, but when you get it back in your code that knows what the second parameter is supposed to be, you’ve got a big problem.

    There are ways to make Generics work as you’d expect without trashing type safety, by placing constraints on the receiving function, but Delphi doesn’t currently implement it. Delphi Prism does, and I’ve been trying to get the Delphi team to implement it in the next release, but we’ll have to see…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I need to implement factory function which returns object O which inherits/has members
I need to implement lastSeq function,which gets as argument string str and char chr
I need to implement a simp^le find function which will retrieve the 1st occurence
I need to implement function which involves long async operation (send request to external
For a homework assignment, I need to implement a function which takes a char
I need to create a function in SQL Server that returns a string. In
The question is Implement a function char* readLine(); which returns single lines from a
There is a good function that I need, which is implemented in the Java
I need to implement a function for moving records up and down (sorting) and
If we need to implement a function that takes an array of integers and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.