Hello everyone!

I have been toying with idea around "conditional" return statement,
that would return, if return result is not nil or false.

First, here is simple example of how should things go with this
new keyword/command. Done using standard Ruby, I think.

|----------------------------------------------------------
| #!/usr/local/bin/ruby
| 
| module TryReturn
|   def tryturn value
|     yield value if value != nil
|   end  
| end
| 
| class Dummy
|   include TryReturn
|   def callIT
|     puts 'Call it'
|     nil
|   end
|   def scientists
|     nil
|   end
|   def deepThought
|     42
|   end
|   def meaningOfLife
|     tryturn( scientists ) {|x| return x}
|     tryturn( deepThought ) {|x| return x}
|     raise TypeError, "There is no answer!"
|   end
| end
| 
| aTest = Dummy.new
| puts 'And the answer is ' + aTest.meaningOfLife.to_s
|----------------------------------------------------------

I'm currently using "tryturn" for its name, but if someone comes up
with better name, I dont mind! :-) [was previously "tryreturn"]

What I would like to see is following code, with no special modules
or "import TryReturn" or "{|x| return x}" blocks.

|----------------------------------------------------------
|   def meaningOfLife
|     tryturn scientists
|     tryturn deepThought
|     raise TypeError, "There is no answer!"
|   end
|----------------------------------------------------------

Actually I have working version of that command (at least it seems
to be working ...) in my own version of ruby. Only four files needs
changes [keywords,parser.y,eval.c,node.h], I think.

I know my example is very simple, too simple to show full power of
that command (or at least I think it has more to offer), but even
simple ones could benefit.

|----------------------------------------------------------
| def doSomethingWith value
|   result = tryFirstThing value
|   return result if result
|   makeSomePreparations value
|   result = trySomethingElse value
|   return result if result
|   raise TypeError, "could not do it"
| end
|----------------------------------------------------------

Or what do you think?

|----------------------------------------------------------
| def doSomethingWith value
|   tryturn tryFirstThing value
|   makeSomePreparations value
|   tryturn trySomethingElse value
|   raise TypeError, "could not do it"
| end
|----------------------------------------------------------

-Jippo

-- 
 __________________________________________________________________
[   pub 1024/951AFAF5 1995/12/12   Juha Pohjalainen <jmp / iki.fi>   ]
[   fingerprint 41 56 7F F9 8E EC 16 35  BB 42 EF A7 DF 19 FA 31   ]
[__ http://www.iki.fi/jmp/ ________________ GSM +358 40 570 1179 __]