Extending Objective-C from D.
Jeremie Pelletier via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jan 17 07:18:47 PST 2016
On Sunday, 17 January 2016 at 13:21:39 UTC, Jeremie Pelletier
wrote:
> On Sunday, 10 January 2016 at 21:51:53 UTC, Guillaume Piolat
> wrote:
>> On Saturday, 9 January 2016 at 10:09:12 UTC, Jeremie Pelletier
>> wrote:
>>> Hello, I'm trying to see if I can write a full Cocoa app in
>>> D, and I'm having trouble creating D classes when the
>>> underlying Objective-C interfaces have methods.
>>>
>>> It works for the app delegate because it needs to override
>>> them, but for other classes the compiler says the Objective-C
>>> methods aren't implemented in the D subclass.
>>>
>>
>> If you are looking to implement/override Obj-C methods in D, I
>> have done it here deriving from NSView
>> https://github.com/p0nce/dplug/blob/master/window/dplug/window/cocoawindow.d#L426
>>
>> Using the Obj-C runtime, the D object declares a new class
>> object, populates it with methods (inverse mapping is done
>> with an instance variable containing "this"). Ugly, but works.
>>
>> Example of a method callback:
>> https://github.com/p0nce/dplug/blob/master/window/dplug/window/cocoawindow.d#L499
>> Do not forget to prepend the callback arguments with id and
>> selector.
>
> Sweet! I can at least get started while the full DIP gets
> implemented :)
>
> I already have the Metal bindings more or less ready to
> publish, I'll try to work on the Cocoa ones this weekend and
> see how much I can progress!
>
> Oh by the way, I see the DIP declares blocks with the __block
> keyword, shouldn't it be better to reuse delegate with
> extern(Objective-C) ? Feels cleaner and more consistent with
> the rest of the language to me.
Turns out I can't execute most of my bindings yet :(
I used interface inheritance all over the place to fully
reproduce Cocoa and Metal, and while it does compile correctly,
calling a objc method from an interface inherited through another
interface causes a segmentation fault.
For example:
interface NSObjectProtocol {
// dispose method here
}
interface NSString : NSObjectProtocol {}
If I get an instance of NSString and call its dispose method, the
process segfaults. If I lift the dispose method into NSString it
works correctly.
At this point I'll wait for more support, there's just too much
to copy/paste :)
More information about the Digitalmars-d
mailing list