Being dim #2: No generic global procedures

The other day I was writing some unit tests and needed some helper functions to look up some array elements of in arrays of different base types. I had several overloaded functions to do the job, like this:

function IndexOf(const Item: string; const A: array of string): Integer; overload;
function IndexOf(const Item: Integer; const A: array of Integer): Integer; overload;

Oh, I thought, generics are a way round this, so I replaced the overloaded functions with something like

function IndexOf<T>(const Item: T; const A: array of T): Integer;

only to be get compiler error 2530: Type parameters not allowed on global procedure or function. Now, I didn't know that and I'm sharing it in case some of you didn't either.

And the solution? I sorted it by using a static class to hold the code, i.e.

  TSomeClass = class(TObject)
    class function IndexOf<T>(const Item: T; const A: array of T): Integer;

This works fine.

I've not provided implementation details of any of this code since the details are not important in this context.


  1. This comment is not specific to this particular blog entry. However, I just want you to know how much I appreciate your blogs and your web site.


  2. Thanks Max - much appreciated.


  3. Anonymous2:34 pm

    still the same in XE5

    strange limitation - I wonder if there's a "good reason" for it...

  4. Dunno why, or even if there's a good reason. Maybe someone is trying to stop us using procedural code!


Post a Comment

Comments are very welcome, but please don't comment here if:

1) You have a query about, or a bug report for, one of my programs or libraries. Most of my posts contain a link to the relevant repository where there will be an issue tracker you can use.

2) You have a query about any 3rd party programs I feature, please address them to the program's developer(s) - there will be a link in the post.

3) You're one of the tiny, tiny minority who are aggressive or abusive - in the bin you go!


Popular posts from this blog

New String Property Editor Planned For RAD Studio 12 Yukon 🤞

Multi-line String Literals Planned For Delphi 12 Yukon🤞

Call JavaScript in a TWebBrowser and get a result back