Hello guys

Hello guys,

Could does someone give me a clue about how to simulate Objective-C's optional methods in Delphi? It is just a proof-of-concept :)

Thanks in advance :)

Comments

  1. Can you give more context?

    You can have base class (not abstract) that implements empty virtual methods you can override later on as-needed bases.

    If you have to mix optional with non optional methods, you can declare non-optional methods as abstract.

    You can also use method references (in way event handlers are used) and you would call them only if they are assigned.

    Hard to tell... Delphi does not have equivalent and it really depends on your use case.

    ReplyDelete
  2. Closest language equivalent is Delphi's messaging syntax, which involves dynamic dispatch. However, there isn't the pseudo-support for arbitrary method signatures you get in Objective-C (I say 'pseudo-support', because without the thick system of conventions that Objective-C developers generally stick to, ordinary Objective-C code would crash a lot due to the lack of runtime checking).

    ReplyDelete
  3. Why not use an interface?
    Then use Supports to check if a class supports that interface?

    Something like

    IRequired = interface
    [guid]
    --nothing here
    end;

    IOptional = interface(IRequired)
    [guid]
    function Method1: boolean;
    end;

    ....

    if Support(MyClass, IOptional, Optional) then Result:= Optional.Method1;

    ReplyDelete
  4. Johan Bontes Interfaces are the equivalent of protocols in Objective-C - I'm sure he will correct me if I'm wrong, but my guess is that Horácio is looking for something to map actual Objective-C optional method declarations, rather than a high-level functional equivalent.

    That said, now I think about it, dispatch interfaces specifically (dispinterface - remember that?) are technically similar to Objective-C protocols to the extent there is no v-table, and method calls are instead dispatched by dispatch identifier (Delphi dispinterface) or selector (Objective-C protocol) instead. Also, like with Objective-C protocols historically, dispinterfaces don't really exist at runtime, so you're free to declare methods that not all implementors of the interface generally actually implement. Obvious downside though is that you need to implement IDispatch!

    ReplyDelete
  5. Creating a COM server interface is really easy and will get you IDispatch implementation as a by-product.
    Just use the build in tool for this: File -> New -> Automation object

    You can then call your functions using the variant.method syntax, just like you'd do when using VBA objects from Word or Excel.

    ReplyDelete
  6. I would love to maintain type safety :) Thanks guys for every help!

    ReplyDelete
  7. Horácio Filho It is still not quite clear what you want to do... or from where you are coming...

    I assume that Chris Rolliston idea was the closest one...

    ReplyDelete
  8. Hello Dalija Prasnikar, sorry for the long reply.

    I want to implement only some methods of a interface, making some them optional, mimicking the behavior of Objective-C' optional methods. The suggested ideas are good however I lose the type safety. I would like to declare my class is still conformant to some interface even without implementing every method of it. I know it can be done using dynamic dispatch however I will end up needing to remove the static interface inheritance from the classes and rely on dynamic casting.

    ReplyDelete

Post a Comment