I am trying to create a function with a variant record-type parameter that allows inline-casting or assignment, as such:
type rectype = ( VT_INT, VT_CHAR, VT_BOOL );
rec = record
case t : rectype of
VT_INT : ( i : integer );
VT_CHAR : ( c : char );
VT_BOOL : ( b : boolean );
end;
procedure handler( r : rec );
begin
case r.t of
VT_INT : { do something with r.i }
VT_CHAR : { do something with r.c }
VT_BOOL : { do something with r.b }
end;
end;
Now, the above "works" fine, as long as you take the time to do set up the variable manually, as such:
r.t := VT_INT;
r.i := 42;
handler( r );
But, I would like to abuse the typing system a bit, and attempt to do it inline (I'll explain why in a moment), along the lines of:
handler( rec( VT_INT, 42 ) );
A helper function would be fine, as well, and I've tried a couple of different methods to do this, which can be seen here (for brevity of this post):
http://pastie.org/private/glxhwbpsbbh5gtxju0uvxa
Now, for the reason: I am working on, and have indeed released a unit testing suite that is aiming to be as portable as Pascal itself (builds under FreePascal and Turbo Pascal 7 (yes, really)). I've already publicly released the open-source software (can't link yet, not enough rep), which include different functions for different types: isI(), isR(), isS(), isP(), isC(), etc. This repeats a lot of code, and I know there is a better way to do this. I believe there is a variant type supported by FPC and Delphi, which I can use via IFDEF directives on those platforms, but the real clincher is TP7, which I still want to support for obscene reasons.
The reason that 4 lines per function call to set up the record isn't really feasible is that as this is the user-facing API, and making testing that convoluted will just mean that no one will do it. A test set with the current API is straightforward with a single function call to perform each test, and I just hope it's possible to turn all of the several type-specific functions into something like:
is( VT_INT, SomeIntFunc( v ), 42, 'Test Name' );
I am willing to go to great lengths to circumvent typing under TP7, including manipulating the stack myself in assembly, etc. But I'm hoping a modified version using syntax I'm just not familiar with will do the job.
So, old-school Pascal programmers (I know there must be some out there), any suggestions?