From: Stephen Duncan Date: 2007-07-20T08:35:20+09:00 Subject: Re: RCR? Added syntax for chains that possibly return nil ------=_Part_115780_2316252.1184888103361 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Not sure if the existence of features in other languages is generally considered a good support for RCR's, but Groovy has this feature: http://groovy.codehaus.org/Statements#Statements-Safenavigation (though the syntax can't be borrowed) -Stephen On 7/19/07, Stefan Rusterholz wrote: > > One type of construct in ruby that somewhat annoys me and IMHO reduces > readability is working around cases where an intermediate method might > return nil. Those cases are e.g. > > while (line = gets && line.chomp) > foo = (bar && bar.baz && bar.baz.quuz) || default # bar might be nil, so > might bar.baz and bar.baz.quuz > > There are some more constructs, most of you surely already used such > stuff or just spread it onto a bit more of lines. > While all of this is fine, maybe that could be improved. My RCR would be > to introduce a new syntax besides . for method invocation, which on the > first nil aborts any further method calls on it and returns nil. > > E.g. the above examples would become: > while line = gets->chomp > foo = bar->baz->quuz || default > > -> was the first best that came to my mind, if you have better ideas, > please state. > Unlike e.g. making nil to respond to every method returning self, this > solution would put the coder in power as to where he cares about getting > nil (e.g. he wants an exception if foo is nil, so he just writes > foo.bar, if he doesn't want one, he writes foo->bar) and where he just > wants to get the nil value back. > > Do you think this RCR is worth to make? > > Regards > Stefan > > -- > Posted via http://www.ruby-forum.com/. > > -- Stephen Duncan Jr www.stephenduncanjr.com ------=_Part_115780_2316252.1184888103361--