forwardingTargetForSelector vs. MacRuby 0.4
Hi all, I'm using a forwardingTargetForSelector: to create a wrapper class in ObjC. It seems to work in ObjC, but when I try to use it in MacRuby 0.4, I get an error. As usual, it's likely I'm doing something quite foolish. Since it's kind of a complicated setup, rather than trying to describe code via email, I've created a minimal-ish project in github containing ObjC and MacRuby code to illustrate the problem. (I say minimal-ish, since I'm using XCode). It's here: http://github.com/diffengr/macruby_forwarding_invocation_example That contains an XCode project and a rakefile. It has the ObjC classes Candy and CandyWrapper, and they both have the NSString* property flavor. CandyWrapper declares that property @dynamic, and forwards it to Candy. Here's the code that works in ObjC. Candy* rootBeerCandy = [[Candy alloc] init]; rootBeerCandy.flavor = @"Root Beer"; CandyWrapper* rootBeerWrapper = [[CandyWrapper alloc] initWithCandy:rootBeerCandy]; NSLog(@"Candy#flavor \"%@\", Wrapper#flavor \"%@\"", rootBeerCandy.flavor, rootBeerWrapper.flavor); If you load and run the xcode project in this directory, you'll get this output: NSLog[...] <CandyWrapper: 0x104fb0> forwarding flavor to <Candy: 0x103380> NSLog[...] Candy#flavor "Root Beer", Wrapper#flavor "Root Beer" Here's that (I think) same code instantiated in MacRuby, from candy_test.rb: rootBeerCandy = Candy.alloc.init rootBeerCandy.flavor = 'Root Beer' rootBeerWrapper = CandyWrapper.alloc.initWithCandy rootBeerCandy puts "Candy#flavor #{rootBeerCandy.flavor}, Wrapper#flavor #{rootBeerWrapper.flavor}" You can run this with "macrake test" in the project directory, and it will give (among other things): candy_test.rb:7:in `<main>': undefined method `flavor' for #<CandyWrapper:0x8000aa2a0> (NoMethodError) rake aborted! So, what am I doing wrong? Thanks, Clay Bridges
Hi Clay, (Sorry for the late reply.) I reproduce the same bug with trunk. I suspect a problem in the dispatcher. We should file this in as a macruby spec. Did you try using the "old" methodSignatureForSelector: and - forwardInvocation: technique? This could work in the meantime the bug is fixed I think. It's what NSProxy uses I think and it works well with 0.4 and trunk. Laurent On Aug 15, 2009, at 11:13 AM, Clay Bridges wrote:
Hi all,
I'm using a forwardingTargetForSelector: to create a wrapper class in ObjC. It seems to work in ObjC, but when I try to use it in MacRuby 0.4, I get an error. As usual, it's likely I'm doing something quite foolish.
Since it's kind of a complicated setup, rather than trying to describe code via email, I've created a minimal-ish project in github containing ObjC and MacRuby code to illustrate the problem. (I say minimal-ish, since I'm using XCode). It's here:
http://github.com/diffengr/macruby_forwarding_invocation_example
That contains an XCode project and a rakefile. It has the ObjC classes Candy and CandyWrapper, and they both have the NSString* property flavor. CandyWrapper declares that property @dynamic, and forwards it to Candy.
Here's the code that works in ObjC.
Candy* rootBeerCandy = [[Candy alloc] init]; rootBeerCandy.flavor = @"Root Beer";
CandyWrapper* rootBeerWrapper = [[CandyWrapper alloc] initWithCandy:rootBeerCandy]; NSLog(@"Candy#flavor \"%@\", Wrapper#flavor \"%@\"", rootBeerCandy.flavor, rootBeerWrapper.flavor);
If you load and run the xcode project in this directory, you'll get this output:
NSLog[...] <CandyWrapper: 0x104fb0> forwarding flavor to <Candy: 0x103380> NSLog[...] Candy#flavor "Root Beer", Wrapper#flavor "Root Beer"
Here's that (I think) same code instantiated in MacRuby, from candy_test.rb:
rootBeerCandy = Candy.alloc.init rootBeerCandy.flavor = 'Root Beer'
rootBeerWrapper = CandyWrapper.alloc.initWithCandy rootBeerCandy puts "Candy#flavor #{rootBeerCandy.flavor}, Wrapper#flavor #{rootBeerWrapper.flavor}"
You can run this with "macrake test" in the project directory, and it will give (among other things):
candy_test.rb:7:in `<main>': undefined method `flavor' for #<CandyWrapper:0x8000aa2a0> (NoMethodError) rake aborted!
So, what am I doing wrong?
Thanks,
Clay Bridges _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
(Sorry for the late reply.)
Ditto.
Did you try using the "old" methodSignatureForSelector: and -forwardInvocation: technique?
Thanks for the suggestion, but no. I'm using MacRuby to test my ObjC classes, and the design decisions on the ObjC side are made othogonal to MacRuby considerations. Put another way, for me, the workarounds should be on the MacRuby side. I actually found some interesting stuff while trying to create a workaround, but I'll post that in another thread. Two follow-up questions: * Do I need to file a bug, etc.? Let me know what I can do to help. * The probably-too-frequent question: Any idea if this will be fixed in 0.5? Thanks Clay Bridges
On Aug 21, 2009, at 9:10 AM, Clay Bridges wrote:
(Sorry for the late reply.)
Ditto.
Did you try using the "old" methodSignatureForSelector: and -forwardInvocation: technique?
Thanks for the suggestion, but no. I'm using MacRuby to test my ObjC classes, and the design decisions on the ObjC side are made othogonal to MacRuby considerations. Put another way, for me, the workarounds should be on the MacRuby side.
I understand that the problem is in the MacRuby side, but you will have to wait until it's fixed (or help us fixing it). Using the "old" technique might work in the interim.
I actually found some interesting stuff while trying to create a workaround, but I'll post that in another thread. Two follow-up questions:
* Do I need to file a bug, etc.? Let me know what I can do to help.
Filing a bug would be great. It would also be great if you could prepare a MacRuby spec that describes this behavior. It could be in spec/macruby/language/method_spec.rb.
* The probably-too-frequent question: Any idea if this will be fixed in 0.5?
We will try! Laurent
participants (2)
-
Clay Bridges
-
Laurent Sansonetti