[MacRuby-devel] Conforming to a protocol

Martijn Walraven martijn at martijnwalraven.com
Wed Nov 17 03:19:05 PST 2010


Thanks for opening a ticket and describing the issue so well! 

I'm not sure how this should be solved, but I was wondering how things currently work for other C structs like NSRect or NSPoint. Are these handled as special cases, or is there a more general way to deal with C structs?

Would it make sense to think about somehow mapping C structs to the Ruby Struct class, or maybe a special CStruct class? It would be nice if this at least offered a way to perform equality checks (==, eql?, equals?). For structs that have defined attributes it would be great if this allowed getting and setting attribute values (similar to what you can do with NSRect and NSPoint).

I might be totally off, so maybe someone who knows more about the internals of MacRuby can comment?

On Nov 17, 2010, at 11:33 , Thibault Martin-Lagardette wrote:

> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20101117/48ddd0e6/attachment.html>


More information about the MacRuby-devel mailing list