Thread overview
Objective C protocols
May 16, 2020
John Colvin
May 17, 2020
John Colvin
May 17, 2020
Jacob Carlborg
May 17, 2020
Guillaume Piolat
May 16, 2020
What's the best way to implement an Objective C protocol in D?

I see mention here https://dlang.org/changelog/2.085.0.html#4_deprecated_objc_interfaces but it's not clear where things are these days.
May 17, 2020
On Saturday, 16 May 2020 at 19:14:51 UTC, John Colvin wrote:
> What's the best way to implement an Objective C protocol in D?
>
> I see mention here https://dlang.org/changelog/2.085.0.html#4_deprecated_objc_interfaces but it's not clear where things are these days.

Based on some experimentation, I'm starting to wonder do protocols actually have any runtime component in Objective C? Because if I pass in an extern(Objective-C) class with the right interface to a function expecting a protocol everything just works.
May 17, 2020
On Saturday, 16 May 2020 at 19:14:51 UTC, John Colvin wrote:
> What's the best way to implement an Objective C protocol in D?
>
> I see mention here https://dlang.org/changelog/2.085.0.html#4_deprecated_objc_interfaces but it's not clear where things are these days.

I did it throught the Obj-C runtime a while ago:
https://github.com/AuburnSounds/Dplug/blob/dda1f80d69e8bfd4af0271721738ce827c2f0eae/au/dplug/au/cocoaviewfactory.d#L99

and the result is brittle, you need to replicate the protocol declaration, add all methods etc.
May 17, 2020
On 2020-05-17 11:32, John Colvin wrote:
> On Saturday, 16 May 2020 at 19:14:51 UTC, John Colvin wrote:
>> What's the best way to implement an Objective C protocol in D?
>>
>> I see mention here https://dlang.org/changelog/2.085.0.html#4_deprecated_objc_interfaces but it's not clear where things are these days.

It's the same these days. It's still not implemented.

> Based on some experimentation, I'm starting to wonder do protocols actually have any runtime component in Objective C?

No, not really.

> Because if I pass in  an extern(Objective-C) class with the right interface to a function expecting a protocol everything just works.

Yes, that works fine.

You can put the methods from the protocol directly in the class that implements them or in a base class. If you really want to have a specific type for the protocol you can use an abstract class to emulate an interface/protocol and cast your actual class to the abstract class:

extern (Objective-C)
abstract class Printer // the protocol
{
    void print(int value) @selector("print:");
}

extern (Objective-C)
class Foo : NSObject
{
    override static Foo alloc() @selector("alloc");
    override Foo init() @selector("init");

    void print(int value) @selector("print:")
    {
        writeln(value);
    }
}

extern (Objective-C) void print(Printer);

void main()
{
    auto foo = Foo.alloc.init;
    print(cast(Printer) cast(void*) foo); // need to cast through void*
}

-- 
/Jacob Carlborg