From: Rick DeNatale Date: 2006-09-26T06:12:41+09:00 Subject: Re: Riddle me this (a question about expressions) On 9/25/06, dblack@wobblini.net wrote: > Hi -- > > On Tue, 26 Sep 2006, Rick DeNatale wrote: > > > On 9/25/06, dblack@wobblini.net wrote: > >> Hi -- > >> > >> On Mon, 25 Sep 2006, Rick DeNatale wrote: > >> > >> > On 9/24/06, dblack@wobblini.net wrote: > >> >> > >> >> I am fairly certain that the return value can never be captured. At > >> >> least I can't figure out how to do it. > >> >> > >> >> In this: > >> >> > >> >> def raise_value > >> >> x = raise > >> >> rescue > >> >> x > >> >> end > >> >> > >> >> p raise_value # nil > >> >> > >> >> I'm pretty sure that x is nil because of the thing where the parser > >> >> sees an assignment and defines the variable. I don't think an > >> >> assignment ever actually takes place -- as witness the fact that: > >> >> > >> >> x = raise || 1 > >> >> > >> >> also leaves x as nil. > >> > > >> > But try: > >> > > >> > x = raise rescue nil || 1 > >> > > >> > Of course this is a trivial example, but the point is that raise can > >> > in fact have a value. > >> > >> Even more trivial: > >> > >> x = raise rescue 1 > >> > >> :-) But I'd still say that the return value of the call to raise is > >> not what's being captured in x. > > > > Well, from a syntax point of view, it sure LOOKs like the rvalue of > > the assignment is the expression "raise rescue 1" > > It is; that expression evaluates to 1, and that's what x gets. But I > don't think the call to raise is, itself, returning 1. > > > David > > -- > David A. Black | dblack@wobblini.net > Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] > DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] > [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com > [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org Regardless of what happens under the covers with setjmp/longjmp, I can't see anyway to explain the results syntactically other than saying that the 'raise .. rescue ...' is an rvalue in the assigment. The genesis of this thread was why the ruby compiler disallows: x = return y but allows x = raise rescue 1 And some have expressed the opinion that both should be disallowed. I can't agree. I think that the proper documentation of Kernel#raise/Thread#raise should be that it causes a non-local return unless it is rescued, in which case the value is the value of the expression in the rescue. That's how it works now. Of course the final arbiter is Matz. -- Rick