Generic Global Functions Well Almost

As mentioned earlier, you cannot declare a generic global function, but you can have a generic class with a class method, which is very close. This is a sample declaration, taken from the TypeCompRules demo: type

TGlobalFunction<T> = class publ ic class function AlmostGlobal: string; class function WithParam (tl72: T): string;


There isn't much you can do inside a similar class method (at least unless you use constraints, covered later in this chapter), so I wrote some code using special generic type functions (again covered later), which is not relevant to discuss here.

You can call various versions of this "global generic function" as follows:




If you call the method with a parameter, however, the parameter's type must match the generic type declaration. So the first two lines below compile, the latter two won't:

TGlobalFunction<TButton>.WithParam (btnGlobal); TGlobalFunction<string>.WithParam ('foo');

TGlobalFunction<Integer>.WithParam (btnGlobal); // [Error] TGlobalFunction<string>.WithParam (203); // [Error]

72 When I first wrote this code, probably with a reminiscence of my C++ days, I wrote the parameter as (t: T). Needless to say in a case insensitive language like Object Pascal, this is not a great idea. The compiler will actually let it go but issue errors every time you refer to the generic type T.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment