[MacRuby-devel] Return from thread context

Laurent Sansonetti lsansonetti at apple.com
Fri Jul 10 13:02:52 PDT 2009


On Jul 10, 2009, at 12:44 PM, Perry Smith wrote:

> On Jul 10, 2009, at 1:58 PM, Laurent Sansonetti wrote:
>
>> Indeed, raising an exception is very slow for us since we use C++  
>> exceptions, but  it's only used in exceptional cases, or in very  
>> explicit use cases like returning from a block (the other return  
>> statements don't use an exception).  Also the new runtime in the  
>> upcoming version of Mac OS X seems to be faster.
>
> I'm not sure we are both on the same page yet.
>
> In your examples:
>
> 1.times { p 42 }
>
> after the "p 42" there is a return.  Perhaps not a return statement  
> but a return.  Also, there is no ensure and no way to put an ensure  
> in without a "begin / end" pair.

In this example there is obviously no return statement, so it will be  
cheap.

The block value or the break statement don't use this mechanism.

> So, what I'm suggesting is to make:
>
> begin
>  foo
>  return here
> rescue
>  dog
> end
>
> be the special case.  Not:
>
> 1.times { return 18 }
>
> I'm guessing that is what you are doing but I wasn't sure.

What about the following:

def foo
   1.times { return 42 }
   p :nok
end
foo

In this case, you need a way to return the current method from the  
block to not execute the "p :nok" expression.

Unwinding the stack is a way to implement this.

Now, consider:

def foo
   begin
     yield
   ensure
     p :ok
   end
end
def bar
   foo { return 42 }
end
p bar

Here you don't have a choice, since you need to honor the ensure block.

In Ruby, return is semantically similar to an exception in some cases.  
This is unfortunate :-)

Laurent


More information about the MacRuby-devel mailing list