jastrachan | 22 Apr 10:22 2004
Picon

Re: Lang request: ||= operator

This is quite neat. I think someone (sorry can't remember who it was, 
Chris? Yuri?) made a similar suggestion a while back for something like 
this - I think using ?= to be like the ternary operator.

  (y != null) ? y : "default"

could be written as

y ?= "default"

On 21 Apr 2004, at 15:31, Thomas Heller wrote:
> Jim Menard wrote:
>> I'd like to request the "||=" operator. In Ruby,
>>     x = nil
>>     x ||= 3
>> assigns 3 to x because x is nil (and, in Ruby, nil returns false in 
>> boolean operations).
>>     x = 42
>>     x ||= 3
>> does not change the value of x in Ruby because x is non-nil (true). 
>> Note that in Ruby
>>     x = 0
>>     x ||= 42
>> does not assign 42 to x because in Ruby 0 is non-nil.
>>     x ||= value
>> could be interpreted as
>>     if (x == nil || x == false) { x = value }
>> Thoughts?
>
> I'd love that. Some time ago Yuri had a similar idea for GStrings
>
> http://jira.codehaus.org/secure/ViewIssue.jspa?key=GROOVY-287
>
> to avoid "null" inside expression this might be a very neat way to 
> work with default values. So instead of "cooking" some custom GString 
> solution we might simply use this operator.
>
> ${user->lastLogin ||= "Never"}
>
> /thomas

James
-------
http://radio.weblogs.com/0112098/

Gmane