Objective C protocols
Jacob Carlborg
doob at me.com
Sun May 17 12:04:31 UTC 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
More information about the Digitalmars-d-learn
mailing list