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 8847513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:12:26+00:00 2026-06-14T12:12:26+00:00

Using Delphi 7, I have several functions that take a single integer parameter. These

  • 0

Using Delphi 7, I have several functions that take a single integer parameter. These functions operate on a Text string belonging to the object. The only parameter is either a character index in the Text, or a word index. For example, to retrieve the next word of Text after word i or after the character Text[loc] I want to have:

function NextWord(i:Integer): String; overload;
function NextWord(loc:Integer): String; overload;

Obviously, this is ambiguous. Delphi Help says “Overloaded routines must be distinguished by the number of parameters they take or the types of their parameters.” So I defined two types:

type WordIndex = Integer;
type CharIndex = Integer;
...
function NextWord(i:WordIndex): String; overload;
function NextWord(i:CharIndex): String; overload;

But this doesn’t work, as these are just aliases for Integer.

Delphi Help on types distinguishes ‘type identity’, ‘type compatibility’, and ‘assignment-compatibility’, and then says nothing about how to use the distinction. But it does say to repeat the word ‘type’ to create new type not identical to Integer:

type WordIndex = type Integer;    //really, a new type
type CharIndex = type Integer;
...
function NextWord(i:WordIndex): String; overload;
function NextWord(i:CharIndex): String; overload;
...
var WordNumber: WordIndex;      //variable of new type
...
  WordStr := NextWord(WordNumber);  //call overloaded function

To my surprise, this doesn’t work either. The compiler recognizes WordIndex as a separate type, and compiles the overloaded functions, but where called it claims the overload is ambiguous. I also tried changing the function parameters to VAR (because “For var parameters, types of formal and actual must be identical.”), but that didn’t help.

Hitting the Web I read that the compiler disambiguates overloaded functions based first on the number of parameters, and second on their size. I suppose I could make WordIndex a DWord instead of Integer, but I don’t want it to be 16 bits, and I’m using -1 to mean ‘invalid word number’.

Finally, I found this in the Help for ‘Overloading procedures and functions’: “You can pass to an overloaded routine parameters that are not identical in type with those in any of the routine’s declarations, but that are assignment-compatible with the parameters in more than one declaration. … In these cases, when it is possible to do so without ambiguity, the compiler invokes the routine whose parameters are of the type with the smallest range that accommodates the actual parameters in the call.”

So I tried this, which works:

type WordIndex = -1..High(Integer);
type CharIndex =  Integer;
function NextWord(i:WordIndex): String; overload;
function NextWord(i:CharIndex): String; overload;
...
var WordNumber: WordIndex;
    loc: CharIndex;
...
  WordStr := NextWord(WordNumber);
  (or)
  WordStr := NextWord(loc);

This compiles without warnings or errors, and does in fact call the correct function for each parameter type. Also SizeOf(WordIndex) = SizeOf(CharIndex) = 4. I.E. they’re both Integers.

My question is, is this how I’m supposed to do it? And if so, how was I supposed to know that? Are there any examples like that in the Help or in the Source?

Bonus question: I checked, and making type WordIndex a Range creates no code/runtime overhead in the call or in the functions. But might it elsewhere in the code? Will Delphi be testing WordNumber for a Range Error anywhere it wouldn’t be testing an Integer? (I’m pretty sure the answer is ‘No’.)

  • 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-06-14T12:12:27+00:00Added an answer on June 14, 2026 at 12:12 pm

    Trying to use overloading here just leads to pain and suffering. Just imagine what happens when you want to use an integer literal? Or imagine you want to pass NextWord an index that is an expression, e.g. a+b. Or you pass the return value of a function. Are you going to have functions that return the various dedicated types that you propose to define? And think about any human that has to read the code. How would they be expected to determine which overload is being called?

    You can avoid all the pain by giving your functions different names. Overloading is appropriate when you have multiple functions that take parameters with different types. That is not the case here. Your functions take parameters with the same type. And so your functions need different names.

    Another way to organise it would be to have a single function, but to pass more to the function. Pass the index, and an enumerated type which describes how to interpret that index. You could combine those two values into a record.

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

Sidebar

Related Questions

I'm using Delphi 2010 on Windows 7 and have a problem with single quotes
I have a Delphi 2009 application that runs a query over a database using
I have written an application (using Delphi 2009) that allows a user to select
i'm using delphi,i have a text and i want to fit it into a
I am using Delphi 2010 to create a Windows service that will monitor several
We have an application developed using Delphi 5 that we cannot upgrade to Delphi
I have an application that's compiled using Delphi 2006, and I want to launch
I have just started a new project, and I am using the Delphi 2009
I have developed an database application with Delphi XE2 using an Access DB, now
I have started using of generics in Delphi 2010 but I have a problem

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.