Search code examples
delphicompiler-directives

Can I deduce the type of a type in a compiler directive


Is it possible to create a conditional define like so:

{$if typeof(TNode) = record}
type PNode = ^TNode;
{$else}
type PNode = TNode;
{$end}

Why do I want this?
I'm alternating between using class and record for a specific problem.
I want to use record for speed reasons, but also want to use class for convenience.

For this reason I'm switching between the two.
Obviously I can add a {$define} statement, but it would be nice to be able to automate this.


Solution

  • Although I personally recommend the general DEFINE approach, you might be successful in those cases where the record is not of a specific size:

    {$if Sizeof(TNode) <> Sizeof(Pointer)}
    type PNode = ^TNode;
    {$else}
    type PNode = TNode;
    {$end}
    

    OK, I know that is dirty programming, but you asked for it in the first place.