[MacRuby] #712: Feature Request: Add Support for C-Level Blocks
#712: Feature Request: Add Support for C-Level Blocks -------------------------------+-------------------------------------------- Reporter: cwdinfo@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: blocker | Milestone: MacRuby 0.7 Component: MacRuby | Keywords: block -------------------------------+-------------------------------------------- The following (proposed) syntax should work: {{{ open_panel = NSOpenPanel.openPanel open_panel.beginSheetModalForWindow(@main_window, completionHandler:lambda{|arg| puts "Hello, open file and user arg is #{arg}" } ) }}} Also, this should work: {{{ def handler(arg) puts "Hello, open file and user arg is #{arg}" end open_panel = NSOpenPanel.openPanel open_panel.beginSheetModalForWindow(@main_window, completionHandler:handler) }}} There may be a more elegant Rubyish way to do this, but I'm not seeing how the proper selector syntax would be created. Here's what I mean: {{{ open_panel = NSOpenPanel.openPanel open_panel.beginSheetModalForWindow(@main_window) do |arg| puts "Hello, open file and user arg is #{arg}" end }}} But the last example gives no hint that the block selector should be completionHandler: -- Ticket URL: <http://www.macruby.org/trac/ticket/712> MacRuby <http://macruby.org/>
#712: Feature Request: Add Support for C-Level Blocks -------------------------------+-------------------------------------------- Reporter: cwdinfo@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: blocker | Milestone: MacRuby 0.7 Component: MacRuby | Keywords: block -------------------------------+-------------------------------------------- Comment(by martinlagardette@…): I would say the 2nd and 3rd form wouldn't be good to implement. The 3rd one because, well, some parts are missing (the `completionHandler:` part). The second one because this looks more like a callback, rather than a Proc/lambda, and would have unexpected behavior from a ruby point of view. In fact, Ruby-wise, it would be expected that "handler" is called and its result is passed to completion handler. Otherwise, I agree 100% for the first form :-). I wanted to do it when implementing the Proc as callback functions, but we had no C-block supports at the time. I hope we can bring them to life (since a C-block is not that far from a ruby-block, at least in principle). -- Ticket URL: <http://www.macruby.org/trac/ticket/712#comment:1> MacRuby <http://macruby.org/>
#712: Feature Request: Add Support for C-Level Blocks -------------------------------+-------------------------------------------- Reporter: cwdinfo@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: blocker | Milestone: MacRuby 0.7 Component: MacRuby | Keywords: block -------------------------------+-------------------------------------------- Comment(by cwdinfo@…): Replying to [comment:1 martinlagardette@…]:
I would say the 2nd and 3rd form wouldn't be good to implement.
The 3rd one because, well, some parts are missing (the `completionHandler:` part). The second one because this looks more like a callback, rather than a Proc/lambda, and would have unexpected behavior from a ruby point of view. In fact, Ruby-wise, it would be expected that "handler" is called and its result is passed to completion handler.
Otherwise, I agree 100% for the first form :-). I wanted to do it when implementing the Proc as callback functions, but we had no C-block supports at the time. I hope we can bring them to life (since a C-block is not that far from a ruby-block, at least in principle).
I understand that the 2nd form appears more like a callback, but the expectation of a functional programming language -- which Ruby debatably is -- is that a Proc is a first-class citizen. Therefore, a method should be usable where a Proc or a lambda is. Regarding the third, how about syntax like: {{{ open_panel.beginSheetModalForWindow(@main_window, completionHandler:MACRUBY_BLOCK) do |arg| puts "Hello, open file and user arg is #{arg}" end }}} The benefit is that you don't wind up shoehorning all your code inside the parentheses of a method call. Dangling close parens are syntax errors waiting to happen. -- Ticket URL: <http://www.macruby.org/trac/ticket/712#comment:2> MacRuby <http://macruby.org/>
#712: Feature Request: Add Support for C-Level Blocks -------------------------------+-------------------------------------------- Reporter: cwdinfo@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: blocker | Milestone: MacRuby 0.7 Component: MacRuby | Keywords: block -------------------------------+-------------------------------------------- Comment(by martinlagardette@…): For the 2nd form, that's not really how ruby works. If lambdas and Procs exist, it is for a reason. One of these reasons being that a method is not an object. It's not a first-class citizen. Procs are. What you can do, however, is call `method(:handler)` to get a Method object. Whatever you do, in ruby, `handler` will call the `handler` method and use its result as the object being referenced. As for the 3rd form, I like it, but we'll have to see how (and if) it is possible to implement it :-). -- Ticket URL: <http://www.macruby.org/trac/ticket/712#comment:3> MacRuby <http://macruby.org/>
#712: Feature Request: Add Support for C-Level Blocks -------------------------------+-------------------------------------------- Reporter: cwdinfo@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: blocker | Milestone: MacRuby 0.7 Component: MacRuby | Keywords: block -------------------------------+-------------------------------------------- Comment(by jazzbox@…): my suggestion is: {{{ open_panel = NSOpenPanel.openPanel open_panel.beginSheetModalForWindowOnCompletion(@main_window) do |arg| puts "Hello, open file and user arg is #{arg}" end }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/712#comment:4> MacRuby <http://macruby.org/>
#712: Feature Request: Add Support for C-Level Blocks -------------------------------+-------------------------------------------- Reporter: cwdinfo@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: blocker | Milestone: MacRuby 0.7 Component: MacRuby | Keywords: block -------------------------------+-------------------------------------------- Comment(by lsansonetti@…): I think only the 1st form should be implemented at the first time. Passing the block inline (3rd form) might be a good idea but I'm afraid of inconsistencies (since the argument name is no longer passed). Also, it wouldn't work if the method accepts 2 blocks. 2nd solution cannot be implemented easily and I think it's not what users would expect (they would expect the method to be called and its return value to be passed, not the method itself). -- Ticket URL: <http://www.macruby.org/trac/ticket/712#comment:5> MacRuby <http://macruby.org/>
#712: Feature Request: Add Support for C-Level Blocks -------------------------------+-------------------------------------------- Reporter: cwdinfo@… | Owner: lsansonetti@… Type: enhancement | Status: new Priority: blocker | Milestone: MacRuby 0.7 Component: MacRuby | Keywords: block -------------------------------+-------------------------------------------- Comment(by demallien@…): As a MacRuby user (and not a dev, for the moment), I definitely prefer that the MacRuby syntax sticks as closely as possible to the Objective-C Cocoa APIs. From my point of view, it's best to leave the rubyfication of the APIs to HotCocoa. -- Ticket URL: <http://www.macruby.org/trac/ticket/712#comment:6> MacRuby <http://macruby.org/>
participants (1)
-
MacRuby