July 12, 2012
"Jonas Drewsen" , dans le message (digitalmars.D:172039), a écrit :
> On Wednesday, 11 July 2012 at 11:18:21 UTC, akaz wrote:
>> if needed, the operator !! (double exclamation mark) could be defined.
>>
>> ...
> 
> Or the operator?? could be borrowed from c#
> 
> auto a = foo ?? new Foo();
> 
> is short for:
> 
> auto a = foo is null ? new Foo() : foo;

or maybe:
auto a = ! ! foo ? foo : new Foo();

|| could be redifined to have this a behavior, but it would break code.

July 12, 2012
Christophe Travert, dans le message (digitalmars.D:172047), a écrit :
> "Jonas Drewsen" , dans le message (digitalmars.D:172039), a écrit :
>> On Wednesday, 11 July 2012 at 11:18:21 UTC, akaz wrote:
>>> if needed, the operator !! (double exclamation mark) could be defined.
>>>
>>> ...
>> 
>> Or the operator?? could be borrowed from c#
>> 
>> auto a = foo ?? new Foo();
>> 
>> is short for:
>> 
>> auto a = foo is null ? new Foo() : foo;
> 
> or maybe:
> auto a = ! ! foo ? foo : new Foo();

I forgot to mention that foo would be evaluated only once (and the second operand would be evaluated lazily). This is the main point of this syntax, and it is not easily emulable (as long a lazy is not fixed).

July 12, 2012
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
July 12, 2012
On Thursday, 12 July 2012 at 12:51:38 UTC, Jacob Carlborg wrote:
> 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.

+1
July 12, 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)

July 12, 2012
"Jonas Drewsen" <jdrewsen@nospam.com> wrote in message news:zwtvliaunccmtwmabxfz@forum.dlang.org...
>
> auto a = foo ?? new Foo();
>
> is short for:
>
> auto a = foo is null ? new Foo() : foo;
>
> /Jonas
>

Yeah, I've been planning to try and get this into D one day.  Probably
something like:
(a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)


July 12, 2012
On Wednesday, 11 July 2012 at 18:21:33 UTC, David Piepgrass wrote:
>>>>> it is just an idea, i do not have any specific use in mind.
> ...
>> But we can't base the decision solely on this fact. Then we could add a million operators to the language just because they seem neat.
>
> Actually, we could! Great idea, nimrod! (inside joke)

ML, Haskell, Clean, Prolog, Mercury, Scala, F# ...


July 12, 2012
On 7/12/12 12:24 PM, Daniel Murphy wrote:
> "Jonas Drewsen"<jdrewsen@nospam.com>  wrote in message
> news:zwtvliaunccmtwmabxfz@forum.dlang.org...
>>
>> auto a = foo ?? new Foo();
>>
>> is short for:
>>
>> auto a = foo is null ? new Foo() : foo;
>>
>> /Jonas
>>
>
> Yeah, I've been planning to try and get this into D one day.  Probably
> something like:
> (a ?: b) ->  (auto __tmp = a, __tmp ? __tmp : b)

gcc used to have that extension and they dropped it...

Andrei

July 12, 2012
On 12-07-2012 19:32, Andrei Alexandrescu wrote:
> On 7/12/12 12:24 PM, Daniel Murphy wrote:
>> "Jonas Drewsen"<jdrewsen@nospam.com> wrote in message
>> news:zwtvliaunccmtwmabxfz@forum.dlang.org...
>>>
>>> auto a = foo ?? new Foo();
>>>
>>> is short for:
>>>
>>> auto a = foo is null ? new Foo() : foo;
>>>
>>> /Jonas
>>>
>>
>> Yeah, I've been planning to try and get this into D one day. Probably
>> something like:
>> (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)
>
> gcc used to have that extension and they dropped it...
>
> Andrei
>

But on the other hand, C# has had it from day one and it's still widely used and encouraged today: http://msdn.microsoft.com/en-us/library/ms173224(v=vs.110).aspx

I find it to be a useful little feature when I have to deal with possibly-null values.

-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
July 12, 2012
On 2012-07-12 18:24, Daniel Murphy wrote:

> Yeah, I've been planning to try and get this into D one day.  Probably
> something like:
> (a ?: b) -> (auto __tmp = a, __tmp ? __tmp : b)

We need to combine it with the assignment operator as well:

Object foo;
foo ?:= new Object; // only assign if "foo" is null

-- 
/Jacob Carlborg