Note that there are some explanatory texts on larger screens.

plurals
  1. POOld-school Pascal question about how to cast variant record function parameters properly
    primarykey
    data
    text
    <p>I am trying to create a function with a variant record-type parameter that allows inline-casting or assignment, as such:</p> <pre><code>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; </code></pre> <p>Now, the above "works" fine, as long as you take the time to do set up the variable manually, as such:</p> <pre><code> r.t := VT_INT; r.i := 42; handler( r ); </code></pre> <p>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:</p> <pre><code> handler( rec( VT_INT, 42 ) ); </code></pre> <p>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):</p> <p><a href="http://pastie.org/private/glxhwbpsbbh5gtxju0uvxa" rel="nofollow noreferrer">http://pastie.org/private/glxhwbpsbbh5gtxju0uvxa</a></p> <p>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.</p> <p>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:</p> <pre><code>is( VT_INT, SomeIntFunc( v ), 42, 'Test Name' ); </code></pre> <p>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.</p> <p>So, old-school Pascal programmers (I know there must be <em>some</em> out there), any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload