just an idea (!! operator)

Christophe Travert travert at phare.normalesup.org
Thu Jul 12 07:13:12 PDT 2012


Jacob Carlborg , dans le message (digitalmars.D:172056), a écrit :
> On 2012-07-12 13:35, Jonas Drewsen wrote:
> 
>> Or the operator?? could be borrowed from c#
>>
>> auto a = foo ?? new Foo();
>>
>> is short for:
>>
>> auto a = foo is null ? new Foo() : foo;
>>
>> /Jonas
>>
> 
> I really like that operator. The existential operator, also known as the 
> Elvis operator :) . It's available in many languages with slightly 
> different semantics.
> 
> -- 
> /Jacob Carlborg

Sweet.

| Elvis Operator (?: )
|
| The "Elvis operator" is a shortening of Java's ternary operator. One 
| instance of where this is handy is for returning a 'sensible default' 
| value if an expression resolves to false or null. A simple example 
| might look like this:
|
| def displayName = user.name ? user.name : "Anonymous" //traditional 
| ternary operator usage
|
| def displayName = user.name ?: "Anonymous"  // more compact Elvis 
| operator - does same as above


(taken from 
http://groovy.codehaus.org/Operators#Operators-ElvisOperator)



More information about the Digitalmars-d mailing list