Feature #4840

Allow returning from require

Added by Rodrigo Rosenfeld Rosas almost 3 years ago. Updated 12 months ago.

[ruby-core:36785]
Status:Feedback
Priority:Normal
Assignee:Yusuke Endoh
Category:-
Target version:next minor

Description

I have a situation where there is some code in Gitorious like:

unless defined? GitoriousConfig
# tons of lines here
end

And I would it to look like

return if defined? GitoriousConfig

#tons of lines here

It would be great to allow return from a required file or some other keyword (break, etc or a new one)

feature-4840.odp - Slide for presentation to Matz (114 KB) Rodrigo Rosenfeld Rosas, 06/08/2012 02:38 AM

0001-compile.c-toplevel-return.patch Magnifier (1.71 KB) Nobuyoshi Nakada, 05/09/2013 11:14 AM

History

#1 Updated by Magnus Holm almost 3 years ago

I think using "return" is quite natural in this case, as long as we
can use it in module/class-definitions too.

// Magnus Holm

On Mon, Jun 6, 2011 at 16:18, Michael Edgar adgar@carboni.ca wrote:

On Jun 6, 2011, at 10:11 AM, Rodrigo Rosenfeld Rosas wrote:

It would be great to allow return from a required file or some other keyword (break, etc or a new one)

This could be implemented as a method that raises an exception that require and load rescue.
I'm not sure how compatible that would be with custom require implementations (rubygems,
polyglot, etc), but it would obviate the need for a new keyword or commandeering an existing one.

Michael Edgar
adgar@carboni.ca
http://carboni.ca/

#2 Updated by Clifford Heath almost 3 years ago

On 07/06/2011, at 12:18 AM, Michael Edgar wrote:

On Jun 6, 2011, at 10:11 AM, Rodrigo Rosenfeld Rosas wrote:

It would be great to allow return from a required file or some

other keyword (break, etc or a new one)

This could be implemented as a method that raises an exception that

require and load rescue.
I'm not sure how compatible that would be with custom require

implementations (rubygems,
polyglot, etc)

Polyglot will pass all exceptions except LoadError (or a subclass).
With a LoadError, if it has no further possibility to satisfy the

require,
the original exception is re-raised. Thus, polyglot should not impede
the implementation you propose.

Clifford Heath.

#3 Updated by Konstantin Haase almost 3 years ago

How will that work with require? Remember it will only load the file once. Return false otherwise? (Which would be kinda compatible with the current behavior and using raise/throw). Or should those values be cached? If you want to use require CommonJS-style, it has to be cached. But what about return values that depend on or provoke side effects? Should files support early return?

Konstantin

On Jun 7, 2011, at 00:08 , Clifford Heath wrote:

On 07/06/2011, at 12:18 AM, Michael Edgar wrote:

On Jun 6, 2011, at 10:11 AM, Rodrigo Rosenfeld Rosas wrote:

It would be great to allow return from a required file or some

other keyword (break, etc or a new one)

This could be implemented as a method that raises an exception that

require and load rescue.
I'm not sure how compatible that would be with custom require

implementations (rubygems,
polyglot, etc)

Polyglot will pass all exceptions except LoadError (or a subclass).
With a LoadError, if it has no further possibility to satisfy the

require,
the original exception is re-raised. Thus, polyglot should not impede
the implementation you propose.

Clifford Heath.

#4 Updated by Yusuke Endoh almost 3 years ago

Hello,

2011/6/6 Rodrigo Rosenfeld Rosas rr.rosas@gmail.com:

I have a situation where there is some code in Gitorious like:

unless defined? GitoriousConfig
 # tons of lines here
end

And I would it to look like

return if defined? GitoriousConfig

#tons of lines here

It would be great to allow return from a required file or some other keyword (break, etc or a new one)

Agreed. It would be also useful to write platform-specific code:

require "test/unit"

return unless /mswin|cygwin|mingw|bccwin/

#5 Updated by Rodrigo Rosenfeld Rosas almost 3 years ago

Em 07-06-2011 07:10, Yusuke ENDOH escreveu:

Hello,

...

Agreed. It would be also useful to write platform-specific code:

require "test/unit"

return unless /mswin|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM

class TestForWindowsEnv< Test::Unit::TestCase
...

Here is an experimental patch:

diff --git a/compile.c b/compile.c
index 10d63bc..7b9c490 100644
--- a/compile.c
+++ b/compile.c
@@ -4291,10 +4291,6 @@ iseqcompileeach(rbiseqt *iseq, LINKANCHOR
*ret, NODE * node, int poped)
rb
iseq_t *is = iseq;

 if (is) {
  • if (is->type == ISEQTYPETOP) {
  • COMPILEERROR((ERRORARGS "Invalid return"));
  • }
  •    else {
     LABEL *splabel = 0;
    
     if (is->type == ISEQ_TYPE_METHOD) {
    

    @@ -4321,7 +4317,6 @@ iseqcompileeach(rbiseqt *iseq, LINKANCHOR
    *ret, NODE * node, int poped)
    ADD
    INSN(ret, nd_line(node), pop);
    }
    }

  •    }
    

    }
    break;
    }
    diff --git a/vminsnhelper.c b/vminsnhelper.c
    index f40dfdf..274f45d 100644
    --- a/vminsnhelper.c
    +++ b/vm
    insnhelper.c
    @@ -1561,8 +1561,6 @@ vmthrow(rbthreadt *th, rbcontrolframet *regcfp,
    cfp = RUBY
    VMPREVIOUSCONTROL_FRAME(cfp);
    }

- rbvmlocaljumperror("unexpected return", throwobj, TAGRETURN);

       valid_return:
     pt = dfp;
     }

This patch is so small, that it seems strange that it affects only
requires... Won't it have side effects? Notice that I didn't test it yet.

I agree with you about specific-platform tests use case too.

Thanks for your interest,

Rodrigo.

#6 Updated by Rodrigo Rosenfeld Rosas almost 3 years ago

Em 07-06-2011 05:14, Haase, Konstantin escreveu:

How will that work with require? Remember it will only load the file once. Return false otherwise? (Which would be kinda compatible with the current behavior and using raise/throw). Or should those values be cached? If you want to use require CommonJS-style, it has to be cached. But what about return values that depend on or provoke side effects? Should files support early return?

I was thinking about that yesterday, but I have no idea how this should
work. If an aborted required should return true or false. Or a value
defined by the return, like:

return :aborted if should_abort?

Or if require should accept some block as:

require('some/file'){|returnvalue| dosomethingwith returnvalue }

Really, I have no idea about this!

Best regards,

Rodrigo.

#7 Updated by Yusuke Endoh almost 3 years ago

Hello,

2011/6/7 Rodrigo Rosenfeld Rosas rr.rosas@gmail.com:

This patch is so small, that it seems strange that it affects only
requires... Won't it have side effects? Notice that I didn't test it yet.

I'm not sure.
But surprisingly, the patch passes all tests except one.

--
Yusuke Endoh mame@tsg.ne.jp

#8 Updated by Yukihiro Matsumoto almost 3 years ago

Hi,

In message "Re: Re: [Ruby 1.9 - Feature #4840][Open] Allow returning from require"
on Tue, 7 Jun 2011 19:10:15 +0900, Yusuke ENDOH mame@tsg.ne.jp writes:

|Agreed.

Ah, I understand the request. But returning from outside of a method
makes me so weird.

                        matz.

#9 Updated by Charles Nutter almost 3 years ago

On Tue, Jun 7, 2011 at 7:33 PM, Yukihiro Matsumoto matz@ruby-lang.org wrote:

Ah, I understand the request.  But returning from outside of a method
makes me so weird.

I agree with both the feature and the fact that return outside a
method feels weird. An early termination of a loading file would be
welcome; I've wanted this many times, and always ended up doing the
super-gross "giant if" to accomplish what could be done by a simple
early exit.

I wonder if a core method that does the early return would be a better
option, like Kernel#exit_script. It could be implemented to throw an
exception all requires and loads expect to catch, like
"ExitScriptError" or something. That would seem more consistent than
having return end the script...but not actually be returning anything.

Another option would be to use a different keyword that isn't so tied
to method/proc bodies, like "break"

break if defined? GitoriousConfig

I think I like the exit_script version better, though.

exit_script if defined? GitoriousConfig

  • Charlie

#10 Updated by Adam Prescott almost 3 years ago

On Wed, Jun 8, 2011 at 8:38 AM, Charles Oliver Nutter
headius@headius.comwrote:

exit_script if defined? GitoriousConfig

This could be confusing, if you happen to view the entire program as a
script. You might think that "exit_script" will do the same thing as
"abort". I like the idea, though.

#11 Updated by Ryan Davis almost 3 years ago

On Jun 7, 2011, at 17:33 , Yukihiro Matsumoto wrote:

Hi,

In message "Re: Re: [Ruby 1.9 - Feature #4840][Open] Allow returning from require"
on Tue, 7 Jun 2011 19:10:15 +0900, Yusuke ENDOH mame@tsg.ne.jp writes:

|Agreed.

Ah, I understand the request. But returning from outside of a method
makes me so weird.

How about raising a specific exception that is rescued by #require instead?

class AbortRequire < StandardError; end

alias :original_require :require

def require f
original_require f
rescue AbortRequire
false
end

def

p require 'f' #

#12 Updated by Charles Nutter almost 3 years ago

On Wed, Jun 8, 2011 at 4:22 AM, Ryan Davis ryand-ruby@zenspider.com wrote:

How about raising a specific exception that is rescued by #require instead?

class AbortRequire < StandardError; end
...
raise AbortRequire if defined? GitoriousConfig

That's pretty clean too. Name needs work ;)

I almost suggested a special catch tag that all requires wrap, but
catch/throw is a little arcane for most folks...

  • Charlie

#13 Updated by Alex Young almost 3 years ago

On 08/06/11 01:33, Yukihiro Matsumoto wrote:

Hi,

In message "Re: Re: [Ruby 1.9 - Feature #4840][Open] Allow returning from require"
on Tue, 7 Jun 2011 19:10:15 +0900, Yusuke ENDOH mame@tsg.ne.jp writes:

|Agreed.

Ah, I understand the request. But returning from outside of a method
makes me so weird.

To me, return would imply that the returned value should be passed back
by the original require call, like so:

$ cat a.rb
return 42

$ cat b.rb
p require("a")

$ ruby -I. b.rb
42

That could be really handy, although it's not compatible with my
previous suggestion (currently languishing here:
http://redmine.ruby-lang.org/issues/4523 - any comments?). An
already-loaded file can still be signaled by a nil return value, and a
file could pretend to be already loaded (if that's at all useful) by
choosing nil as its return value...

Food for thought :-)

--
Alex

#14 Updated by Konstantin Haase almost 3 years ago

One real use case I see would be avoiding global state (like CommonJS). However, if an already required file return nil, this is not possible.
On Jun 8, 2011, at 13:57 , Alex Young wrote:

On 08/06/11 01:33, Yukihiro Matsumoto wrote:

Hi,

In message "Re: Re: [Ruby 1.9 - Feature #4840][Open] Allow returning from require"
on Tue, 7 Jun 2011 19:10:15 +0900, Yusuke ENDOH mame@tsg.ne.jp writes:

|Agreed.

Ah, I understand the request. But returning from outside of a method
makes me so weird.

To me, return would imply that the returned value should be passed back
by the original require call, like so:

$ cat a.rb
return 42

$ cat b.rb
p require("a")

$ ruby -I. b.rb
42

That could be really handy, although it's not compatible with my
previous suggestion (currently languishing here:
http://redmine.ruby-lang.org/issues/4523 - any comments?). An
already-loaded file can still be signaled by a nil return value, and a
file could pretend to be already loaded (if that's at all useful) by
choosing nil as its return value...

Food for thought :-)

Alex

#15 Updated by Yusuke Endoh about 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

#16 Updated by Thomas Sawyer about 2 years ago

I've had occasion to use this as well, especially for RUBY_VERSION specific code.

I wonder if it is okay to be embedded in other code though. Would this work?

class Q
def foo
exit_script
end
end

Q.new.foo

Is it a good idea for it to work? Or should exit_script only be allowed at toplevel?

#18 Updated by Yusuke Endoh almost 2 years ago

Received. Thanks for quick action!

But, matz said "returning from outside of a method makes me so weird" once.
I'm ok if you want to give it a second try with no change, but I guess matz is not likely to accept it for the same reason.
(Well, but, maybe it isn't so bad way because he sometimes changes his mind)

Yusuke Endoh mame@tsg.ne.jp

#19 Updated by Rodrigo Rosenfeld Rosas almost 2 years ago

thanks for worrying, but I'll take the risk :)

#20 Updated by Yusuke Endoh over 1 year ago

Rodrigo Rosenfeld Rosas,

At the developer meeting (7/21), Matz was basically positive
to this proposal, but it turned out that we still need to
discuss corner cases.

Here is a discussion summary.

  • Matz said that he will accept "return from toplevel", but
    that reject "return from class definition".

    return if cond # OK
    class Foo
    return if cond # NG
    end

  • "return from toplevel" should always discard the argument.

  • What's happen if it returns from the start script (the given
    script to interpreter as a cmdline)?

    • The argument should be discarded; it does NOT affect the process termination status code
    • The same as "exit", but cannot rescue SystemExit
  • What's happen in the following corner cases?

    • eval("return") in toplevel
    • proc { return }.call in toplevel
  • Matz prefered "return" to "a special exception that require
    and load rescue",

    • though some people (including ko1) prefered the latter.

Yusuke Endoh mame@tsg.ne.jp

#21 Updated by Rodrigo Rosenfeld Rosas over 1 year ago

Em 23-07-2012 10:12, mame (Yusuke Endoh) escreveu:

Issue #4840 has been updated by mame (Yusuke Endoh).

Rodrigo Rosenfeld Rosas,

At the developer meeting (7/21), Matz was basically positive
to this proposal, but it turned out that we still need to
discuss corner cases.

Here is a discussion summary.

  • Matz said that he will accept "return from toplevel", but
    that reject "return from class definition".

    return if cond # OK
    class Foo
    return if cond # NG
    end

Totally agree.

  • "return from toplevel" should always discard the argument.

You mean "return something", right? I agree. Maybe we could even issue
an exception if anything other than just "return" is used.

  • What's happen if it returns from the start script (the given
    script to interpreter as a cmdline)?

    • The argument should be discarded; it does NOT affect the process termination status code
    • The same as "exit", but cannot rescue SystemExit

Agreed. Specifically "return" should be equivalent to "exit 0", right?
And "return -1" (or any other value, including 0) shouldn't be allowed
in my opinion, raising an exception, in which case the return value
wouldn't be 0 obviously :)

  • What's happen in the following corner cases?

    • eval("return") in toplevel

I'd vote for just abandoning any subsequent code in the eval and nothing
else. Maybe for that case "return" could use the arguments for the
result of "eval".

  • proc { return }.call in toplevel

If it is the main program, "exit" would be the equivalent. Otherwise, no
code would be interpreted after the "call" call.

I don't understand what makes this so special.

  • Matz prefered "return" to "a special exception that require
    and load rescue",

    • though some people (including ko1) prefered the latter.

I'm okay with raising an exception that would be rescued by require,
require_relative and load.

Actually I guess this is the simpler approach and it fulfills all the
real use cases I could think of in this moment.

#22 Updated by Alexey Muranov over 1 year ago

How about redefining __END__ to allow to call it as a method?

To avoid ambiguity with DATA constant, maybe __START_DATA__ keyword can be added?

#23 Updated by Koichi Sasada over 1 year ago

(2012/07/24 0:44), alexeymuranov (Alexey Muranov) wrote:

How about redefining __END__ to allow to call it as a method?

It has compatibility issue that END is related to DATA.

--
// SASADA Koichi at atdot dot net

#24 Updated by Koichi Sasada over 1 year ago

(2012/07/23 23:57), Rodrigo Rosenfeld Rosas wrote:

  • "return from toplevel" should always discard the argument.

You mean "return something", right? I agree. Maybe we could even issue
an exception if anything other than just "return" is used.

  • What's happen if it returns from the start script (the given
    script to interpreter as a cmdline)?

    • The argument should be discarded; it does NOT affect the process termination status code
    • The same as "exit", but cannot rescue SystemExit

Agreed. Specifically "return" should be equivalent to "exit 0", right?
And "return -1" (or any other value, including 0) shouldn't be allowed
in my opinion, raising an exception, in which case the return value
wouldn't be 0 obviously :)

matz proposed that ignore return argument completely. matz also said to
avoid mistake, return expression with argument (example: "return foo")
should be syntax error.

  • proc { return }.call in toplevel

If it is the main program, "exit" would be the equivalent. Otherwise, no
code would be interpreted after the "call" call.

I don't understand what makes this so special.

(1)
pr = proc{return}
def foo(pr)
pr.call # what happen?
end

(1')
1.times{
return
}

(2)
# a.rb
$pr = proc{return}

# b.rb
require './a.rb'
$pr.call # waht happen?

(3)
begin
...
rescue
return
ensure
return
end

matz proposed that "accept return on toplevel (not in block, classes, etc)".

  • Matz prefered "return" to "a special exception that require
    and load rescue",

    • though some people (including ko1) prefered the latter.

I'm okay with raising an exception that would be rescued by require,
require_relative and load.

Actually I guess this is the simpler approach and it fulfills all the
real use cases I could think of in this moment.

I strongly recommended the exception approach because there are NO
difficult corner cases.

However, matz disagreed the exception approach because of "raise
StopLoading is too long". It is a kind of "name is not good".

--
// SASADA Koichi at atdot dot net

#25 Updated by Thomas Sawyer over 1 year ago

How about redefining __END__ to allow to call it as a method?

It has compatibility issue that END is related to DATA.

Then what about __end__?

Tangentially, why not deprecate END? Is there some really
important use case that we just can't live without? The whole idea
strikes me as rather hackish, especially considering it is limited to
main file.

#26 Updated by Rodrigo Rosenfeld Rosas over 1 year ago

Em 23-07-2012 22:37, SASADA Koichi escreveu:

(2012/07/23 23:57), Rodrigo Rosenfeld Rosas wrote:

  • "return from toplevel" should always discard the argument.
    You mean "return something", right? I agree. Maybe we could even issue
    an exception if anything other than just "return" is used.

  • What's happen if it returns from the start script (the given
    script to interpreter as a cmdline)?

    • The argument should be discarded; it does NOT affect the process termination status code
    • The same as "exit", but cannot rescue SystemExit Agreed. Specifically "return" should be equivalent to "exit 0", right? And "return -1" (or any other value, including 0) shouldn't be allowed in my opinion, raising an exception, in which case the return value wouldn't be 0 obviously :) matz proposed that ignore return argument completely. matz also said to avoid mistake, return expression with argument (example: "return foo") should be syntax error.

Better yet.

- proc { return }.call in toplevel

If it is the main program, "exit" would be the equivalent. Otherwise, no
code would be interpreted after the "call" call.

I don't understand what makes this so special.
(1)
pr = proc{return}
def foo(pr)
pr.call # what happen?
end

I wonder why someone would write code like this in the first place, but
if someone did he is probably expecting the program to terminate with
exit if this is the main file. For required files I have no idea what
someone would expect from code like this. I'd probably raise an
exception in such situation because it is most likely a logic error that
could be hard to debug... I know what you mean, should that method "foo"
be defined or not? What should be its definition? I have no idea because
code like this simply doesn't make any sense to me.

(1')
1.times{
return
}

This one I can think of. But something like:

['a', 'b'].each {|item|
return if defined? item2module(item)
}

This would simply return from the require. I still don't understand why
this would be so special.

(2)
# a.rb
$pr = proc{return}

Wow! I wouldn't ever think in something like this! Congratulations,
you're really creative!

Again, why the hell would someone do something like this? I'd just raise
an error when trying to call $pr because return has no more meaning
after that file has been already required.

# b.rb
require './a.rb'
$pr.call # waht happen?

(3)
begin
...
rescue
return
ensure
return
end

matz proposed that "accept return on toplevel (not in block, classes, etc)".

I agree, this is way more simple to deal with. Even though I can think
about someone using an approach like above, he could also rewrite it like:

# ['a', 'b'].each {|item|
# return if defined? item2module(item)
#}

return if ['a', 'b'].any?{|item| defined? item2module(item) }

  • Matz prefered "return" to "a special exception that require
    and load rescue",

    • though some people (including ko1) prefered the latter. I'm okay with raising an exception that would be rescued by require, require_relative and load.

Actually I guess this is the simpler approach and it fulfills all the
real use cases I could think of in this moment.
I strongly recommended the exception approach because there are NO
difficult corner cases.

Agreed.

However, matz disagreed the exception approach because of "raise
StopLoading is too long". It is a kind of "name is not good".

So it would be just a matter of finding a better name, right? Not that I
think it should make any difference because ideally only Ruby internals
should see such errors in my opinion.

But if he thinks StopLoading is too long (while I find it short) it will
be hard to find a shorter meaningful name I guess.

Maybe after a good night of sleep, who knows...

#27 Updated by Shyouhei Urabe over 1 year ago

On 2012年07月24日 11:42, Trans wrote:

How about redefining __END__ to allow to call it as a method?

It has compatibility issue that END is related to DATA.

Then what about __end__?

Tangentially, why not deprecate END? Is there some really
important use case that we just can't live without? The whole idea
strikes me as rather hackish, especially considering it is limited to
main file.

I'd be much appreciated if END was usable from a library file.

I'd write a tiny loader script and place obfuscated script body on END then.

#28 Updated by Eric Hodel over 1 year ago

On Jul 23, 2012, at 7:42 PM, Trans transfire@gmail.com wrote:

why not deprecate END? Is there some really
important use case that we just can't live without? The whole idea strikes me as rather hackish, especially considering it is limited to main file.

I infrequently use END for single-file scripts. It's easier to transport a single file with embedded data than two or more files.

#29 Updated by Yusuke Endoh over 1 year ago

  • Target version set to 2.0.0

#30 Updated by Yusuke Endoh over 1 year ago

Anyone create a patch conformed to the spec written in ?

I guess that my experimental patch () is not confirmed completely;
perhaps it allows "return from class definition" (but I didn't tested yet).

Yusuke Endoh mame@tsg.ne.jp

#31 Updated by Yutaka HARA about 1 year ago

  • Status changed from Assigned to Feedback
  • Assignee changed from Yukihiro Matsumoto to Yusuke Endoh
  • Target version changed from 2.0.0 to next minor

#32 Updated by Nobuyoshi Nakada 12 months ago

A simple patch.

#33 Updated by Rodrigo Rosenfeld Rosas 12 months ago

Pretty simple, indeed :) Not that I understand it, just that I appreciate its simplicity ;)

#34 Updated by Matthew Kerwin 12 months ago

On May 9, 2013 12:14 PM, "nobu (Nobuyoshi Nakada)" nobu@ruby-lang.org
wrote:

Issue #4840 has been updated by nobu (Nobuyoshi Nakada).

File 0001-compile.c-toplevel-return.patch added

A simple patch.

Line 369 has a typo in "rescue"

#35 Updated by Rodrigo Rosenfeld Rosas 12 months ago

Em 09-05-2013 13:27, Rodrigo Rosenfeld Rosas escreveu:

Em 09-05-2013 10:19, Matthew Kerwin escreveu:

On May 9, 2013 12:14 PM, "nobu (Nobuyoshi Nakada)"
> wrote:

Issue #4840 has been updated by nobu (Nobuyoshi Nakada).

File 0001-compile.c-toplevel-return.patch added

A simple patch.

Line 369 has a typo in "rescue"

I noticed it, but I thought it was optional to raise in case it hasn't
returned previously...

Sorry, I meant intentional, not optional.

#36 Updated by Rodrigo Rosenfeld Rosas 12 months ago

Em 09-05-2013 10:19, Matthew Kerwin escreveu:

On May 9, 2013 12:14 PM, "nobu (Nobuyoshi Nakada)" > wrote:

Issue #4840 has been updated by nobu (Nobuyoshi Nakada).

File 0001-compile.c-toplevel-return.patch added

A simple patch.

Line 369 has a typo in "rescue"

I noticed it, but I thought it was optional to raise in case it hasn't
returned previously...

Also available in: Atom PDF