[MacRuby-devel] Conforming to a protocol

Thibault Martin-Lagardette thibault.ml at gmail.com
Wed Nov 17 03:44:05 PST 2010


These structures are currently handled by Foundation's BridgeSupport file (/System/Library/Frameworks/Foundation.framework/Resources/BridgeSupport/Foundation.bridgesupport)
	<struct name='NSPoint' type64='{CGPoint=&quot;x&quot;d&quot;y&quot;d}' type='{_NSPoint=&quot;x&quot;f&quot;y&quot;f}'/>
	<struct name='NSRange' type64='{_NSRange=&quot;location&quot;Q&quot;length&quot;Q}' type='{_NSRange=&quot;location&quot;I&quot;length&quot;I}'/>
It's not very humanly readable, but MacRuby understands what this means, and then knows NSPoint is a structure :-).

However, just for proving myself wrong, there IS a Protocol Obj-C objet ( see http://opensource.apple.com/source/objc4/objc4-437.1/runtime/Protocol.h ).
But I think my point stands, as I do think what is returned is the C struct, not the class.
I think Laurent might know a little better though :-)

-- 
Thibault Martin-Lagardette



On Nov 17, 2010, at 12:19, Martijn Walraven wrote:

> 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
> 
> _______________________________________________
> 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/51aa645c/attachment.html>


More information about the MacRuby-devel mailing list