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.
type TSomeClass = class(TObject) public class function IndexOf<T>(const Item: T; const A: array of T): Integer; end;
This works fine.
I've not provided implementation details of any of this code since the details are not important in this context.
2 comments:
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.
Max
Thanks Max - much appreciated.
Peter
Post a Comment