Duck typing in Delphi 2007?

前端 未结 3 1842
你的背包
你的背包 2020-12-05 16:02

Question:

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


Duck typing Re

3条回答
  •  暖寄归人
    2020-12-05 16:44

    Here's an idea that requires you create a type library.

    Use OLE Automation types, and implement Dispatch Interfaces (dual COM objects).

    Now you Now you can write whatever you want after that type, and we'll be finding out at runtime whether or not it works, or blows up. Welcome to dynamic typing.

    procedure DoSomething(D:OleVariant);
    begin
      D.Quack; // Might work, might blow up.
    end;
    

    I consider it ugly, but others might not.

提交回复
热议问题