[MacRuby-devel] Conforming to a protocol
Thibault Martin-Lagardette
thibault.ml at gmail.com
Wed Nov 17 02:33:55 PST 2010
This is because protocols, in the Obj-C runtime, are not Obj-C objets per say, they are C structs.
+protocolWithName returns an (id) (aka obj-c objet), but the actual returned pointer is just a pointer to a C struct, which causes the runtime to issue those warnings. It says "Hey, this method returned an objet, but it doesn't look like one!". Which is expected, but this should be improved.
While it is true that in the Obj-C runtime, classes and objects are C structs too, they are obviously not the same kind of structures, which is why it doesn't work :-).
In MacRuby, `Protocol` IS a real Obj-C objet, but not what the +protocolWithName method returns. This means that whatever you do with the returned valiue, it will crash, because it is not a real objet, and thus does not respond to any message.
This also means that you cannot even do something like that:
Protocol.protocolWithName("NSCoding") == Protocol.protocolWithName("NSCoding")
Simply because doing this will call the `#==` method on the left-most value, which is a C struct for a protocol, and not an Obj-C object.
I created https://www.macruby.org/trac/ticket/999 , related to protocols.
Please be aware that the attached patch still does not make it possible to override conformsToProtocol:, because calling `#==` on non-objets will crash, which is why I think MacRuby could handle Protocols a little better, right now I'm not sure it's "usable" per say.
Sorry if I do repeat myself a little, but I want to make sure you understand why this does not work yet, and what you can and cannot do with protocols as of today :-).
--
Thibault Martin-Lagardette
On Nov 17, 2010, at 09:26, Martijn Walraven wrote:
> I actually tried this, but the problem seems to be that Protocol is not a true class. So if I try to do anything with the protocol parameter from MacRuby, I get an error:
>
> *** NSInvocation: warning: object 0x7fff71192488 of class 'Protocol' does not implement methodSignatureForSelector: -- trouble ahead
> *** NSInvocation: warning: object 0x7fff71192488 of class 'Protocol' does not implement doesNotRecognizeSelector: -- abort
>
> Even just comparing it to another protocol object using protocol == Protocol.protocolWithName('...') leads to the same result.
>
> Any ideas as to how this could be made to work?
>
> On Nov 16, 2010, at 19:59 , Eloy Duran wrote:
>
>> I don't have an example of a class that uses conformsToProtocol: on the delegate, so I can't give you a code example, but I would try to override the conformsToProtocol: class and instance methods and return true for those you support.
>>
>> On 15 nov 2010, at 00:15, Martijn Walraven wrote:
>>
>>> Hi,
>>>
>>> I was wondering if there is any way to formally indicate a MacRuby class conforms to an Objective-C protocol. I encountered some code that uses conformsToProtocol: instead of respondsToSelector: as a check before invoking delegate methods, and the only way I could get a delegate written in MacRuby to work was to create an Objective-C class with the same name and specify the required protocols in the interface declaration there.
>>>
>>> Thanks,
>>>
>>> Martijn
>>> _______________________________________________
>>> MacRuby-devel mailing list
>>> MacRuby-devel at lists.macosforge.org
>>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20101117/728e5a0d/attachment.html>
More information about the MacRuby-devel
mailing list