Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7587137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:35:56+00:00 2026-05-30T19:35:56+00:00

Question: Is there a way to do duck typing with Delphi 2007 (i.e. without

  • 0

Question:

Is there a way to do duck typing with Delphi 2007 (i.e. without generics and advanced Rtti features)?


Duck typing Resources for Delphi 2010 onward:

  • Duck Duck Delphi in google project by ARCANA.
  • Duck Typing in Delphi by Daniele Teti.
  • AOP and duck typing in Delphi by Stefan Glienke.

Last Edit:

I’ve delved more into the resouces listed above and studied every posted answers here.

I end up refining my requirement a made a follow up post to this question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:35:58+00:00Added an answer on May 30, 2026 at 7:35 pm

    With the help of the ObjAuto.pas and invokable variant types it should be possible (written in XE but should also run in Delphi 7 or lower):

    unit DuckTyping;
    
    interface
    
    function Duck(Instance: TObject): Variant;
    
    implementation
    
    uses
      ObjAuto,
      SysUtils,
      TypInfo,
      Variants;
    
    type
      TDuckVarData = packed record
        VType: TVarType;
        Reserved1, Reserved2, Reserved3: Word;
        VDuck: TObject;
        Reserved4: LongWord;
      end;
    
      TDuckVariantType = class(TPublishableVariantType)
      protected
        function GetInstance(const V: TVarData): TObject; override;
      public
        procedure Clear(var V: TVarData); override;
        procedure Copy(var Dest: TVarData; const Source: TVarData;
          const Indirect: Boolean); override;
        function DoFunction(var Dest: TVarData; const V: TVarData;
          const Name: string; const Arguments: TVarDataArray): Boolean; override;
      end;
    
    var
      DuckVariantType: TDuckVariantType;
    
    { TDuckVariantType }
    
    procedure TDuckVariantType.Clear(var V: TVarData);
    begin
      V.VType := varEmpty;
      TDuckVarData(V).VDuck := nil;
    end;
    
    procedure TDuckVariantType.Copy(var Dest: TVarData; const Source: TVarData;
      const Indirect: Boolean);
    begin
      if Indirect and VarDataIsByRef(Source) then
        VarDataCopyNoInd(Dest, Source)
      else
      begin
        with TDuckVarData(Dest) do
        begin
          VType := VarType;
          VDuck := TDuckVarData(Source).VDuck;
        end;
      end;
    end;
    
    function TDuckVariantType.DoFunction(var Dest: TVarData; const V: TVarData;
      const Name: string; const Arguments: TVarDataArray): Boolean;
    var
      instance: TObject;
      methodInfo: PMethodInfoHeader;
      paramIndexes: array of Integer;
      params: array of Variant;
      i: Integer;
      ReturnValue: Variant;
    begin
      instance := GetInstance(V);
      methodInfo := GetMethodInfo(instance, ShortString(Name));
      Result := Assigned(methodInfo);
      if Result then
      begin
        SetLength(paramIndexes, Length(Arguments));
        SetLength(params, Length(Arguments));
        for i := Low(Arguments) to High(Arguments) do
        begin
          paramIndexes[i] := i + 1;
          params[i] := Variant(Arguments[i]);
        end;
    
        ReturnValue := ObjectInvoke(instance, methodInfo, paramIndexes, params);
        if not VarIsEmpty(ReturnValue) then
          VarCopy(Variant(Dest), ReturnValue);
      end
      else
      begin
        VarClear(Variant(Dest));
      end;
    end;
    
    function TDuckVariantType.GetInstance(const V: TVarData): TObject;
    begin
      Result := TDuckVarData(V).VDuck;
    end;
    
    function Duck(Instance: TObject): Variant;
    begin
      TDuckVarData(Result).VType := DuckVariantType.VarType;
      TDuckVarData(Result).VDuck := Instance;
    end;
    
    initialization
      DuckVariantType := TDuckVariantType.Create;
    
    finalization
      FreeAndNil(DuckVariantType);
    
    end.
    

    You can simply use it like this:

    type
      {$METHODINFO ON}
      TDuck = class
      public // works in XE, not sure if it needs to be published in older versions
        procedure Quack;
      end;
    
    procedure TDuck.Quack;
    begin
      ShowMessage('Quack');
    end;
    
    procedure DoSomething(D: Variant);
    begin
      D.Quack;
    end;
    
    var
      d: TDuck;
    begin
      d := TDuck.Create;
      try
        DoSomething(Duck(d));
      finally
        d.Free;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Silly question - Is there a way to download the iPhone SDK without Xcode
Much like this question is there a way to make Visual Studio 2010 prompt
Question Is there a way to have a method that will always run anytime
Question Is there a way to define a method only once in C# (in
Question: Is there a way to check if a given font is one of
Possible duplicate question: Is there a way to indefinitely pause a thread? In my
The real question: Is there a way to clear certain attributes for all components
I have a very theoretical question: Is there a way to ban the use
Question: Is there an easy way (library function) to perform a bitwise AND or
Simple question here: is there any way to convert from a jagged array to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.