Scott Moore wrote:
For example, you can implement C++ style "default parameters" by using overloads ability to match parameter counts.
FWIW, I'll also consider default parameters when dealing with overlaods. They're no less type-strict, and may be more comfortable to use in some situations.
No longer any need to coin various names for WINAPI functions and document them. And the result, IMHO is cleaner. Instead of the C call:
someapifunc(&q, NULL);
I have:
someapifunc(q);
And a simple rule: if the parameter does not exist, leave it out !
Good example indeed. But it is a case where default parameters may be even better suited -- if there are n such parameters, with overloads you'll need 2^n routines.
Typically I have seen this kind of interfacing in other Pascals by allowing for an operator such as &x, that takes the address of any variable, then allowing the user to stuff any address into a pointer parameter. This "solves" the problem, but imports a huge type safety problem from C into Pascal, namely the ability to arbitrarily manipulate addresses.
Taking the address doesn't imply doing address arithmetic. As a thought experiment, just imagine that every global and local variable is internally allocated via `New', every normal use of the variable uses the-internal-pointer^, and `&x' (or `@x' as in BP and GPC) yields this internal pointer. This is no less type-safe (as I just proved, as I described it in standard Pascal terms). Of course, most of the dialects that habe `&'/`@' also allow for address arithmetic which is not so harmless.
The number of traditional Pascal typing difficulties templates solve is stunning. Remember the common complaint about having to have several different procedures or functions to get around fixed length strings ? That problem no longer applies. A collection of string functions can be written for fixed length strings that adapt to any string type.
But in your scheme, each invocation would create a new copy of the routine, which is not really nice WRT compile-time and binary-size. I think I prefer schema parameters for this case which are also type-safe.
How about the ability to write a set of general purpose list manipulators ? In standard Pascal you have to rewrite a set of insert, delete, sort, etc routines to fit each type of dynamic list. With templates, you can write a series of routines that only rely on the common features of a list, such as each entry having a ".next" field, and a ".data" field.
Yes, here templates would come handy.
Frank